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】filterメソッド 条件に一致した要素を取り出す

Last updated at Posted at 2021-04-30

Javascriptについてプラグラミングん勉強を始めた初めの頃に勉強をしたが、全くと言っていいほどなにも覚えていないため復習のためにQiitaにまとめ振り返っていこうと思う。

filterメソッド

filterメソッドとは記述した条件に一致した要素のみ抽出してあらたな配列を作成するメソッド。

// charactersという配列をつくる。
const characters = [
  {id: 1, name:"田中太郎", age: 56},
  {id: 2, name:"小林ゴリラ", age: 1},
  {id: 3, name:"森本ジョンソン", age: 31}
];

// charactersから30歳未満のキャラクターを取り出し、定数underThirtyに代入してください
const underThirty =  characters.filter((character) => {
  return character.age < 30;
});

// underThirtyを出力する。
console.log(underThirty);

出力結果

[ 
  {id: 2, name:"小林ゴリラ", age: 1}
]
0
1
1

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?