๐ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ํ๋ค๊ฐ splice() ํจ์
๋ฅผ ์ ๋๋ก ์์ง ๋ชปํด ๋ค์ MDN๋ฅผ ๊ฒ์ํ๋ ๋์ ๋ชจ์ต์ ๋ณด๊ณ ๋ธ๋ก๊ทธ์ ์ ๋ฆฌํด๋๋ฉฐ ๋ณต์ตํ๊ณ ์ ํ๋ค
splice() ๋ฉ์๋
๋ ๋ฐฐ์ด์ ๊ธฐ์กด ์์๋ฅผ ์ญ์ ๋๋ ๊ต์ฒดํ๊ฑฐ๋ ์ ์์๋ฅผ ์ถ๊ฐํ์ฌ ๋ฐฐ์ด์ ๋ด์ฉ์ ๋ณ๊ฒฝํ๋ค.array.splice(start, deleteCount, plusitem);
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
//['Jan', 'Feb', 'March', 'April', 'June'];
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
let removed = myFish.splice(2,0,'drum','guitar');
//["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
let myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon'];
let removed = myFish.splice(3,1);
//["angel", "clown", "drum", "sturgeon"]
let myFish = ['angel', 'clown', 'drum', 'sturgeon'];
let removed = myFish.splice(2,1,'trumpet');
//["angel", "clown", "trumpet", "sturgeon"]
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
let removed = myFish.splice(0,2,'parrot', 'anemone', 'blue');
//["parrot", "anemone", "blue", "trumpet", "sturgeon"]
let myFish = ['parrot', 'anemone', 'blue', 'trumpet', 'sturgeon'];
let removed = myFish.splice(2,2);
//["parrot", "anemone", "sturgeon"]
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
let removed = myFish.splice(-2,1);
//["angel", "clown", "sturgeon"]
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
let removed = myFish.splice(2);
//["angel", "clown"]