Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- springboot
- sudo
- map is not a function
- Dockerfile
- mysql error
- spring framework
- Access denied for user ''@'localhost'
- jdk
- springboot jar
- brew install mariadb
- react map error
- Oracle
- root
- Docker
- docker container
- ps
- join
- mac mariadb
- DB
- sts
- install
- jar deploy
- 파일 시스템
- jar배포
- systemd
- 관리
- 설치
- 도커파일
- 도커
- mysql
Archives
- Today
- Total
Yoon.s
[javascript] split(), sort(), reverse(), join() 함수 본문
프로그래머스 문제 '문자열 내림차순으로 배치하기'를 풀다가 알게된 함수들을 정리하겠습니다.
이 함수들만 알았어도 빠르게 풀 수 있었던 문제 ㅠㅠ
split()
String 객체를 지정한 구분자를 이용하여 여러 개의 문자열로 나눈다.
- split 괄호 안에 들어가는 것은 구분자
- (1)예제는 구분자 '공백'
- (2)예제는 구분자 '공백없음' -> 한글자씩 떼내겠다는 것
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' '); //(1)
console.log(words[3]);
// expected output: "fox"
const chars = str.split(''); //(2)
console.log(chars[8]);
// expected output: "k"
sort()
배열의 요소를 적절한 위치에 정렬한 후 그 배열을 반환한다.
- 기본적으로는 숫자/ 문자열의 순서에 따라 오름차순으로 정렬되나,
아래 예제처럼 숫자의 길이는 판단하지 못함- 제대로 된 정렬을 원한다면 람다함수를 이용
arr.sort((a, b) => a-b; );
- 제대로 된 정렬을 원한다면 람다함수를 이용
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]
reverse()
배열의 순서를 반전됩니다.
첫 번째 요소는 마지막 요소가 되며 마지막 요소는 첫 번째 요소가 됩니다.
- 변화된 배열의 값들을 reversed에 저장했지만 기존의 함수도 같이 변화함 (주의 !!)
const array1 = ['one', 'two', 'three'];
const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]
console.log('array1:', array1);
// expected output: "array1:" Array ["three", "two", "one"]
join()
배열의 모든 요소를 연결해 하나의 문자열로 만듭니다.
- 구분자를 주지 않으면 상관없이 무조건 다 붙음
- 구분자 (공백없음 , - ) 를 배열의 값들을 붙일 때 중간에 넣어줌
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join());
// expected output: "Fire,Air,Water"
console.log(elements.join(''));
// expected output: "FireAirWater"
console.log(elements.join('-'));
// expected output: "Fire-Air-Water"
참고자료.
developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/split
developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/join
'프론트 > Javascript' 카테고리의 다른 글
[javascript] substr(), substring(), slice() 비교 (0) | 2020.09.09 |
---|---|
[javascript] filter 함수 (0) | 2020.09.07 |
[javascript] reduce() 함수 (1) | 2020.09.02 |
Comments