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']
['j','a','v','a','s','c','r','i','p','t'].slice(1); // ['a','v','a','s','c','r','i','p','t']
['j','a','v','a','s','c','r','i','p','t'].slice(2); // ['v','a','s','c','r','i','p','t']
['j','a','v','a','s','c','r','i','p','t'].slice(0, 1); // ['j']
['j','a','v','a','s','c','r','i','p','t'].slice(1, 2); // ['a']
['j','a','v','a','s','c','r','i','p','t'].slice(0, 2); // ['j','a']
['j','a','v','a','s','c','r','i','p','t'].slice(0, 3); // ['j','a','v']
['j','a','v','a','s','c','r','i','p','t'].slice(5, 10); // ['c','r','i','p','t']
['j','a','v','a','s','c','r','i','p','t'].slice(5, -1); // ['c','r','i','p']
['j','a','v','a','s','c','r','i','p','t'].slice(5, -2); // ['c','r','i']
['j','a','v','a','s','c','r','i','p','t'].slice(-1); // ['t']
['j','a','v','a','s','c','r','i','p','t'].slice(-2); // ['p','t']
['j','a','v','a','s','c','r','i','p','t'].slice(-2); // ['i','p','t']
['j','a','v','a','s','c','r','i','p','t'].slice(-3, -1) // ['i','p']
['j','a','v','a','s','c','r','i','p','t'].slice(-3, -2) // ['i']
['j','a','v','a','s','c','r','i','p','t'].slice(-3, -4) // X
"javascript".slice(0); //javascript
"javascript".slice(1); //avascript
"javascript".slice(2); //vascript
"javascript".slice(0,1); //j
"javascript".slice(1,2); //a
"javascript".slice(0,2); //ja
"javascript".slice(0,3); //jav
"javascript".slice(5,10); //cript
"javascript".slice(5,-1); //crip
"javascript".slice(5,-2); //cri
"javascript".slice(-1); //t
"javascript".slice(-2); //pt
"javascript".slice(-3); //ipt
"javascript".slice(-3,-1); //ip
"javascript".slice(-3,-2); //i
console.log("javascript".slice(0));
30. splice()
"배열".splice(시작위치,[삭제 갯수],[요소])
- 배열의 기존 요소를 삭제 또는 교체하거나 새 요소를 추가하여 배열의 내용을 변경합니다.
['java','script','refer','ence'].splice(1); //['script','refer','ence']
['java','script','refer','ence'].splice(2); //['refer','ence']
['java','script','refer','ence'].splice(3); //['ence']
['java','script','refer','ence'].splice(1, 2); //['script','refer']
['java','script','refer','ence'].splice(1, 3); //['script','refer','ence']
const str = ['java','script','refer','ence'];
const result = str.splice(1, 3, "react");
console.log(str); //['java', 'react']
console.log(result); //['script', 'refer', 'ence']
const arrNum1 = [100, 200, 300, 400, 500];
const result1 = arrNum1.splice(1); //0부터 1개의배열을 삭제
console.log(result1); //[100] 삭제한 값
console.log(arrNum1); //[200, 300, 400, 500] 결과값
const arrNum2 = [100, 200, 300, 400, 500];
const result2 = arrNum2.splice(1, 2); //1부터 2개의배열을 삭제제
console.log(result2); //[200, 300] 삭제한 값
console.log(arrNum2); //[100, 400, 500] 결과값
const arrNum3 = [100, 200, 300, 400, 500];
const result3 = arrNum3.splice(1, 4); //1부터 4개의배열을 삭제
console.log(result3); //[200, 300, 400, 500] 삭제한 값
console.log(arrNum3); //[100] 결과값
const arrNum4 = [100, 200, 300, 400, 500];
const result4 = arrNum4.splice(1, 2, "javascript"); //1부터 2개의배열을 삭제
console.log(result4); //[200, 300] 삭제한 값
console.log(arrNum4); //[100, 'javascript', 400, 500] 결과값
const arrNum5 = [100, 200, 300, 400, 500];
const result5 = arrNum5.splice(1, 0, "javascript"); //1부터 0개의배열을 삭제
console.log(result5); //[] 삭제한 값
console.log(arrNum5); //[100, 'javascript', 200, 300, 400, 500] 결과값
const arrNum6 = [100, 200, 300, 400, 500];
const result6 = arrNum6.splice(0, 4, "javascript"); //0부터 4개의배열을 삭제
console.log(result6); //[100, 200, 300, 400] 삭제한 값
console.log(arrNum6); //['javascript', 500] 결과값
const arrNum7 = [100, 200, 300, 400, 500];
const result7 = arrNum7.splice(2, 2, "javascript", "react"); //2부터 2개의배열을 삭제
console.log(result7); //[300, 400] 삭제한 값
console.log(arrNum7); //[100, 200, 'javascript', 'react', 500] 결과값
const arrNum8 = [100, 200, 300, 400, 500];
const result8 = arrNum8.splice(-2, 2, "javascript"); //-라서 뒤에서부터 2부터 2개의배열을 삭제
console.log(result8); //[400, 500] 삭제한 값
console.log(arrNum8); //[100, 200, 300, 'javascript'] 결과값