29. slice()

"๋ฐฐ์—ด".slice(์‹œ์ž‘์œ„์น˜, [๋๋‚˜๋Š” ์œ„์น˜])
  • ์‹œ์ž‘ ์œ„์น˜์—์„œ ์ข…๋ฃŒ ์œ„์น˜ ๊ฐ’์„ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
{
    ['j','a','v','a','s','c','r','i','p','t'];                      //['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(-3);            //['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']
}

30. splice()

"๋ฐฐ์—ด".slice(์‹œ์ž‘์œ„์น˜, [์‚ญ์ œ ๊ฐฏ์ˆ˜], [์š”์†Œ])
  • ๋ฐฐ์—ด์˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ์‚ญ์ œ ๋˜๋Š” ๊ต์ฒดํ•˜๊ฑฐ๋‚˜ ์ƒˆ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
{
    const arrNum1 = [100, 200, 300, 400, 500];
    const result1 = arrNum1.splice(1);

    console.log(result1);        //[200, 300, 400, 500]
    console.log(arrNum1);        //[100]

    const arrNum2 = [100, 200, 300, 400, 500];
    const result2 = arrNum2.splice(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);

    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");

    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");

    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");

    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");

    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");

    console.log(result8);       //[400, 500]
    console.log(arrNum8);       //[100, 200, 300, "javascript"]

    //result๋Š” ์‚ญ์ œ๋œ ์š”์†Œ
    //arrNum์€ ๋‚˜๋จธ์ง€ + ์ถ”๊ฐ€๋œ ์š”์†Œ
}