プログラミング言語
Javascript
- DOM操作
- パッケージマネージャー(npm)
- メソッド
- 文字列操作
- 演算子
- instanceof
- 数値操作
- 日付と時刻
- 配列
- 関数
- audioクラス
- クラス
- snakeToCamelCase
配列から特定の要素を削除
配列から特定の要素を削除するには、indexOf
とsplice
の二つのメソッドを組み合わせて使います。
const fruits = ['Apple', 'Orange', 'Banana'];
fruits.splice(fruits.indexOf('Orange'), 1);
オブジェクトの配列から、特定の属性を持つオブジェクトを削除したい場合は、findIndex
メソッドを利用します。
const fruits = [
{name: 'Apple', price: 100},
{name: 'Orange', price: 150},
{name: 'Banana', price: 50}
];
fruits.splice(fruits.findIndex(fruit => fruit.name === 'Apple'), 1);