配列から特定の要素を削除


配列から特定の要素を削除するには、indexOfspliceの二つのメソッドを組み合わせて使います。

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);