티스토리 뷰
1. split()
2020/02/26 - [Study/script.js] - [string] split - 문자열 분할
[string] split - 문자열 분할
문자열 분할 메소드 예시 const splitString = (e) => { const split = e.split(' ') return split } console.log(splitString('가나 다라 마바 사')) // ["가나", "다라", "마바", "사"] split의 인자로 어느 기..
andwinter.tistory.com
2.sort()
배열의 정렬이 가능하다.
기본 오름차순 정렬인데, 아래처럼 쓸 수 있다.
let a = ["c", "d", "a"]
console.log(a.sort())
//["a","c","d"]
3. reverse()
배열 뒤집기.
위의 정렬된 배열에 reverse를 쓰면
let a = ["c", "d", "a"]
console.log(a.sort().reverse())
//["d", "c", "a"]
이렇게 반전된다.
예제
입력되는 문자열을 내림차순으로 정렬
const arrSort = s => {
let a = s.split('').sort().reverse().reduce((a,b) => a+b)
console.log(a)
}
arrSort("ceasgwZ")
// "wsgecaZ"
1. 들어온 문자열을 쪼개어 배열로 만든다.(split)
2. 오름차순으로 정렬한다.(sort)
3. 반전시킨다(reverse)
4. 문자 하나하나 들어가있는 배열을 합친다.(reduce)
'Study > script.js' 카테고리의 다른 글
[error]node_modules\path-to-regexp 에러(Cannot read property 'length' of undefined) (0) | 2020.03.19 |
---|---|
[full]mongoDB, mongoose (0) | 2020.02.29 |
[string] split - 문자열 분할 (0) | 2020.02.26 |
[express] View - PUG (0) | 2020.02.24 |
[express] router (0) | 2020.02.22 |
댓글