😸
【JavaScript】splice()
- 配列の途中の要素にアクセスする命令
- 変化した結果の要素が1つであっても配列のまま
// start = 変化する位置のインデックス
// n = 削除する要素の数
// item1, item2, ... = 追加する要素(省略可)
arr.splice(start, n, item1, item2, ...);
const scores = [32, 55, 82, 27];
scores.splice(2, 0, 13, 89);
// [32, 55, 13, 89, 82, 27]
const deleted = scores.splice(3, 1);
// [32, 55, 13, 82, 27]
// deleted = [89]
scores.splice(2, 2, 30);
// [32, 55, 30, 27]
console.log(scores); // [32, 55, 30, 27]
console.log(deleted); // [89]
Discussion