61
61

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 5 years have passed since last update.

もうforループなんていらない!?その2every,some,reduce ついでにアロー関数とlet,constも

Last updated at Posted at 2019-01-27

# 概要
以前投稿した__【もうforループなんていらない!?その1 forEach,map,filter,find】__の続きです。

今回は**「every,some,reduce」についてまとめます。
また前回の投稿で様々な方にアドバイスをいただき
「アロー関数」と「変数宣言(const,let)」**についても勉強してきたので、そちらもコードの中にしっかり書いていきたいと思います!

#every
配列の要素__「全て」__が条件を満たす場合に__true__を、満たさない場合には__false__を返してくれます。

const members = [
    {name: "Naoya", age: 23},
    {name: "Takuya", age: 25},
    {name: "Shinya", age: 19}
];

members.every(member => member.age < 30 ); //true
members.every(member => member.age > 20); //false

#some
配列の要素の内__「1つでも」__条件を満たす場合には__true__を、満たさない場合には__false__を返してくれます。

const members = [
    {name: "Naoya", age: 23},
    {name: "Takuya", age: 25},
    {name: "Shinya", age: 19}
];

members.some(member => member.age > 20); //true
members.some(member => member.age > 30 ); //false

#reduce

reduce() はアキュムレータと配列の各要素に対して(左から右へ)関数を適用し、単一の値にします。

元の配列の要素を蓄積して、新しい単一の値を作るって事ですね!

const books = [
    {title: "ハリーポッター", price: 1500},
    {title: "3匹の子豚", price: 1000}
];

let sumPrice = books.reduce((sum,book) => sum + book.price ,0);

console.log(sumPrice); //2500
61
61
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
61
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?