06. slice()
"배열".slice(시작 위치, [끝나는 위치])
- 시작 위치에서 종료 위치 값을 추출하여, 새로운 배열을 반환합니다.
['j','a','v','a','s','c','r','i','p','t'].slice(0);
['j','a','v','a','s','c','r','i','p','t'].slice(1);
['j','a','v','a','s','c','r','i','p','t'].slice(2);
['j','a','v','a','s','c','r','i','p','t'].slice(0,1);
['j','a','v','a','s','c','r','i','p','t'].slice(1,2);
['j','a','v','a','s','c','r','i','p','t'].slice(0,2);
['j','a','v','a','s','c','r','i','p','t'].slice(0,3);
['j','a','v','a','s','c','r','i','p','t'].slice(5,10);
['j','a','v','a','s','c','r','i','p','t'].slice(5,-1);
['j','a','v','a','s','c','r','i','p','t'].slice(5,-2);
['j','a','v','a','s','c','r','i','p','t'].slice(-1);
['j','a','v','a','s','c','r','i','p','t'].slice(-2);
['j','a','v','a','s','c','r','i','p','t'].slice(-3);
['j','a','v','a','s','c','r','i','p','t'].slice(-3, -1);
['j','a','v','a','s','c','r','i','p','t'].slice(-3, -2);
결과 확인하기
['j','a','v','a','s','c','r','i','p','t']
['a','v','a','s','c','r','i','p','t']
['v','a','s','c','r','i','p','t']
['j']
['a']
['j', 'a']
['j', 'a', 'v']
['c', 'r', 'i', 'p', 't']
['c', 'r', 'i', 'p']
['c', 'r', 'i']
['t']
['p', 't']
['i','p','t']
['i','p']
['i']
['a','v','a','s','c','r','i','p','t']
['v','a','s','c','r','i','p','t']
['j']
['a']
['j', 'a']
['j', 'a', 'v']
['c', 'r', 'i', 'p', 't']
['c', 'r', 'i', 'p']
['c', 'r', 'i']
['t']
['p', 't']
['i','p','t']
['i','p']
['i']
30. splice()
"배열".splice(시작 위치, [삭제 개수], [요소])
- 배열 시작 위치에서 종료 위치 값을 추출하여, 새로운 배열을 반환합니다.
- 원본 파일의 값이 변경됩니다.
const arrNum1 = [100, 200, 300, 400, 500];
const result1 = arrNum1.splice(1);
console.log(arrNum1);
console.log(result1);
-----------------------------------------------------------
const arrNum2 = [100, 200, 300, 400, 500];
const result2 = arrNum2.splice(1, 2);
console.log(arrNum2);
console.log(result2);
-----------------------------------------------------------
const arrNum3 = [100, 200, 300, 400, 500];
const result3 = arrNum3.splice(1, 4);
console.log(arrNum3);
console.log(result3);
-----------------------------------------------------------
const arrNum4 = [100, 200, 300, 400, 500];
const result4 = arrNum4.splice(1, 2, "javascript");
console.log(arrNum4);
console.log(result4);
-----------------------------------------------------------
const arrNum5 = [100, 200, 300, 400, 500];
const result5 = arrNum5.splice(1, 0, "javascript");
console.log(arrNum5);
console.log(result5);
-----------------------------------------------------------
const arrNum6 = [100, 200, 300, 400, 500];
const result6 = arrNum6.splice(0, 4, "javascript");
console.log(arrNum6);
console.log(result6);
-----------------------------------------------------------
const arrNum7 = [100, 200, 300, 400, 500];
const result7 = arrNum7.splice(2, 2, "javascript", "react");
console.log(arrNum7);
console.log(result7);
-----------------------------------------------------------
const arrNum8 = [100, 200, 300, 400, 500];
const result8 = arrNum8.splice(-2, 2, "javascript");
console.log(arrNum8);
console.log(result8);
결과 확인하기
1.
[100] //arrNum
[200, 300, 400,500] // result값은 지워진 값
2.
[100, 400, 500]
[200, 300]
3.
[100]
[200, 300, 400, 500]
4.
[100, "javascript", 400, 500]
[200, 300]
5.
[100, "javascript", 200, 300, 400, 500]
[]
6.
["javascript", 500]
[100, 200, 300, 400]
7.
[100, 200, "javascript", "react", 500]
[300, 400]
8.
[100, 200, 300, "javascript"]
[400, 500]
[100] //arrNum
[200, 300, 400,500] // result값은 지워진 값
2.
[100, 400, 500]
[200, 300]
3.
[100]
[200, 300, 400, 500]
4.
[100, "javascript", 400, 500]
[200, 300]
5.
[100, "javascript", 200, 300, 400, 500]
[]
6.
["javascript", 500]
[100, 200, 300, 400]
7.
[100, 200, "javascript", "react", 500]
[300, 400]
8.
[100, 200, 300, "javascript"]
[400, 500]