mdn에 나온 정의를 살펴 보았다. 단순 복사가 아닌 이해를 하며 기록하였다.
slice() 메서드는 어떤 배열의 begin부터 end까지 ( end 미포함)에 대한 얕은 복사본을 새로운 배열로 반환하며 원본 배열은 바뀌지 않는다.
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"]
splice() 방법은 배열의 기존 요소를 삭제하거나 교체하거나 새 요소를 추가하여 배열의 내용을 변경한다.
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March",
"April", "May"]
삭제하는 경우
let arr = [1,2,3,4,5]
arr.splice(2,1) //ex 2번째 요소를 시작점으로 1개원소 삭제
console.log(arr); //[1,2,4,5]
split() 메서드는 String 객체를 지정한 구분자를 이용하여 여러 개의 문자열로 나눈다.
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' '); //' '한칸
console.log(words[3]);
// expected output: "fox"
const chars = str.split(''); //''한칸X
console.log(chars[8]);
// expected output: "k"
const strCopy = str.split();
console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]