10
8

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】find,filterメソッドの違い、使い方

Last updated at Posted at 2020-05-04

#【JavaScript】find,filterメソッドの違い、使い方

##findメソッド
配列から条件に一致した最初の値を返す。

調査対象の配列.find((引数)=>{
return 条件;
}


    const numbers = [1, 2, 3, 4, 5, 6];
    const foundNumber = numbers.find((number) => {
     return number > 3;
    });
    
    console.log(foundNumber);//4

・findメソッドでは1つしか値を取得できない

引数、条件が1つの場合ここまで省略可能

    const foundNumber = numbers.find(number => number > 3);

##filterメソッド
配列から条件に一致した値をすべて返す。

調査対象の配列.filter((引数)=>{
return 条件;
}


    const numbers = [1, 2, 3, 4, 5, 6];
    const filteredNumber = numbers.filter((number) => {
     return number > 3;
    });
    
    console.log(filteredNumber);//[4, 5, 6]

引数、条件が1つの場合ここまで省略可能

    const filteredNumber = numbers.filter(number => number > 3);
10
8
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
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?