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?

map、filterメソッドの使い方

Posted at

mapメソッドとは?

ある配列に対して、与えられた関数処理を行い、新たな配列を作ります。

filterメソッドとは?

ある配列の、与えられた関数の条件に合致した要素を取り出し、新たな配列を作るメソッド。

具体例

例として、const arry = [10, 20, 30, 40];を定義し、この各要素を2倍にして、50以上の値を取り出すとします。

メソッドを使わない場合

main.js
const newArry = []

for (let i = 0; i < arry.length; i++) {
    const val = arry[i] * 2;
    if (val > 50) {
        newArry.push(arry[i] * 2);
    }
}

map、filterメソッドを使う場合

main.js
const newArry = arry.map(val => val * 2).filter(val => val > 50);

まとめ

メソッドを使い、コードの記述量が減りました。
おわり

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?