0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【JavaScript】オブジェクトを要素に持つ配列で、条件に当てはまる要素を全削除する方法

Last updated at Posted at 2020-01-11

はじめに

JavaScriptの配列操作で、条件に当てはまる要素を全削除する方法を備忘録として記載します。

対象はオブジェクトを要素に持つ配列です。

環境

OS: macOS Catalina 10.15.1

結論

const array = [
  {id: 1, n: 1, m: 2},
  {id: 2, n: 1, m: 3},
  {id: 3, n: 1, m: 2},
];

//上記配列からnが1, mが2のオブジェクトを全削除する。
//つまり、nが1, mが2「でない」配列に置き換える

const newArray = array.filter(item => 
  !(item.n === 1 && item.m === 2)
); 

出力

array;
// => [ { id: 1, n: 1, m: 2 }, { id: 2, n: 1, m: 3 }, { id: 3, n: 1, m: 2 } ] 

newArray;
// => [ { id: 2, n: 1, m: 3 } ] 

おわりに

以下記事を参考に、自分用にメモさせて頂きました:bow_tone1:

参考にさせて頂いたサイト(いつもありがとうございます)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?