0andme.github.io
0andme gitHub

Array.slice()

훼손 없이 배열 자르기
April 14, 2022
  1. js

Array.prototype.slice()

const newArr = Array.slice(start[, end])

start 인덱스부터 end-1 인덱스 까지의 값들을 모아 새로운 배열로 반환한다. end 인덱스가 없다면 start 인덱스 부터 배열의 끝까지.
새로운 배열을 반환하므로 원본 배열은 변하지 않는다.
인덱스가 음수면 뒤에서부터 n번째 인덱스를 의미한다.

✤ 기본 예제
const arr = ["a", "b", "c", "d", "e"]
const newArr = arr.slice(1, 4) // ["b","c","d"]
const newArrB = arr.slice(-2) // ["d","e"]

공식 문서


Profile picture