目的
- 配列(array)から特定の要素(element)を削除したい
array.remove(int);
のようなことをしたい
方法
- 削除したい要素のindexを調べる
splice
でその要素を削除する
サンプル
var array = [4, 4, 8, 4]; var index = array.indexOf(8); if (index > -1) { array.splice(index, 1); } console.log(array); // => [4, 4, 4]
参考リンク
- Array.prototype.splice() | MDN
古い要素を取り除きつつ新しい要素を追加することで、配列の内容を変更します。