Method of Array

wanni kim·2021년 4월 19일
0
post-thumbnail

Array.slice()에 대해서

배열의 어느 시작점 부터 끝점까지에 대한 복사본을 새로운 배열 객체로 반환한다.
"원본 배열은 바뀌지 않는다." immutable

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

시작점(begin) : 0을 시작으로하는 추출 시작점에 대한 인덱스를 의미.
음수로 시작한다면, 배열의 끝에서부터의 길이를 추출.

시작점이 undefined이면 0부터 시작.
배열의 길이보다 시작점의 수가 큰경우는 빈 배열 반환.

종료점(end) : end index를 제외하고 배열을 추출함.
ex) [1, 2, 3, 4] 일때, array.slice(1,3) 이면 [2, 3]만 추출.
end가 생략되면 배열의 끝까지 추출.
end가 배열의 길이보다 커도 끝까지 추출.
end값이 음수일 경우에는 뒤에서 음수의 숫자를 뺀 나머지 배열을 리턴.

profile
Move for myself not for the others

0개의 댓글