728x90

✨ 배열(array)을 원하는 크기 만큼 자르고 싶을 때 사용하는 helper method(원래 값은 유지되며, copy개념이다)
(e.g., arr.slice(0,2)) 왼쪽처럼 시작(0), 끝(2) index를 지정해주면, array의 0부터 1 index까지 값을 얻어낼 수 있다.
slice( '시작', '여기 바로 앞까지' )으로 기억해주면 편하다.
Capitalize, Chunk and Merge Sort Algorithm 에서 유용하게 사용하였다.

💻Example Code

const arr = [ 1, 2, 3, 4, 5 ];

console.log( arr.slice(0,2) );

실행 결과(0부터 1 index까지 출력)

😋 자주 사용하지 않다가 사용하면, 어디까지 array가 잘리는지 헷갈리기 쉬운 helper method이다.

👉 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

Global.parseInt()  (0) 2019.12.24
Math.sign()  (0) 2019.12.24
Math.floor()  (0) 2019.12.23
Number.prototype.toFixed()  (0) 2019.12.22
Function.prototype.apply()  (0) 2019.12.22

+ Recent posts