๐ย Reference
๐ Chapter
arr.slice([begin[, end]]);
๋ฐฐ์ด๋ณ์๋ช
.slice('์ถ์ถํ ์์ ์ธ๋ฑ์ค', '์ถ์ถ์ ์ข
๋ฃํ ์ธ๋ฑ์ค');
begin (optional)
slice(-2)ย ๋ ๋ฐฐ์ด์์ ๋ง์ง๋ง ๋ ๊ฐ์ ์๋ฆฌ๋จผํธ๋ฅผ ์ถ์ถํ๋ค.begin์ดย undefined์ธ ๊ฒฝ์ฐ์๋, 0๋ฒ ์ธ๋ฑ์ค๋ถํฐย slice ํ๋ค.begin์ด ๋ฐฐ์ด์ ๊ธธ์ด๋ณด๋ค ํฐ ๊ฒฝ์ฐ์๋, ๋น ๋ฐฐ์ด์ ๋ฐํํ๋ค.end (optional)
sliceย ๋ย endย ์ธ๋ฑ์ค๋ฅผย ์ ์ธํ๊ณ ์ถ์ถํ๋ค.slice(1,4)๋ ๋๋ฒ์งธ ์์๋ถํฐ ๋ค๋ฒ์งธ ์์๊น์ง (1, 2 ๋ฐ 3์ ์ธ๋ฑ์ค๋ก ํ๋ ์์)ย ์ถ์ถํ๋ค.slice(2,-1)ย ๋ ์ธ๋ฒ์งธ๋ถํฐ ๋์์ ๋๋ฒ์งธ ์์๊น์ง ์ถ์ถํ๋ค.end๊ฐ ์๋ต๋๋ฉดย slice()๋ ๋ฐฐ์ด์ ๋๊น์ง(arr.length) ์ถ์ถํ๋ค.endย ๊ฐ์ด ๋ฐฐ์ด์ ๊ธธ์ด๋ณด๋ค ํฌ๋ค๋ฉด,ย silce()๋ ๋ฐฐ์ด์ ๋๊น์ง(arr.length) ์ถ์ถํ๋ค.arr.slice()
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// ["camel", "duck"]
console.log(animals.slice(1, 5));
// ["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2));
// ["duck", "elephant"]
console.log(animals.slice(2, -1));
// ["camel", "duck"]
console.log(animals.slice());
// ["ant", "bison", "camel", "duck", "elephant"]