0
0

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 1 year has passed since last update.

Array.prototype.filter() について

Posted at

メモです。

Array.prototype.filter()

filter() メソッドは、この配列の中から、提供された関数で実装されたテストに合格した要素のみを抽出したシャローコピーを作成します。

環境

node --version
v14.21.3

検証

配列の各要素に JSON が含まれれるものの中から errorMessage という key が含まれないもの、含まれるものを取得する。

const array = [
    {
        name: 'Bob',
        age: 20
    },
    {
        name: 'Ken',
        age: 30
    },
    {
        errorMessage: "hogefuga"
    }
]

// JSON はいずれの方法でもアクセスできる
// https://developer.mozilla.org/ja/docs/Learn/JavaScript/Objects/JSON
console.log(array[2]['errorMessage']);
console.log(array[2].errorMessage);

// view bob
console.log(array.filter(result => result['errorMessage'] === undefined));

// view error
console.log(array.filter(result => result['errorMessage'] !== undefined));

実行してみる

hogefuga
hogefuga
[ { name: 'Bob', age: 20 }, { name: 'Ken', age: 30 } ]
[ { errorMessage: 'hogefuga' } ]
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?