8
7

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.

【JavaScript・初学者】Reactでよく使われる頻出記法 / filterについて

Last updated at Posted at 2023-11-14

はじめに

Reactを学習していましたが、JavaScriptの記法についてフワッとしている部分がよくあったので、簡単ですが今回はfilterについて少し調べました。

filterの使い方

filter条件に合致した値を配列で返すメソッドです。

let array = [1,2,3,4,5]
array = array.filter((num) => {
    return num >= 2
  }
)
console.log(array) // [2,3,4,5]

オブジェクト配列に対しても使うことができます。

const heroes = [
    {name: Batman, franchise: DC},
    {name: Ironman, franchise: Marvel},
    {name: Thor, franchise: Marvel},
    {name: Superman, franchise: DC}
];

const marvelHeroes =  heroes.filter((hero) => {
    return hero.franchise == Marvel;
});

// [ {name: “Ironman”, franchise: “Marvel”}, {name: “Thor”, franchise: “Marvel”} ]
8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?