LoginSignup
0
3

More than 5 years have passed since last update.

【JavaScript】reduce基礎:配列に入ってる数値を順に引いていく

Last updated at Posted at 2017-04-14

JavaScriptの勉強の備忘録として書き留めておきます。

reduce

配列全体を変換する。例えば全要素の合計を出したり、平均値を出したり出来る。
結果がアキュムレータに入り、それが戻り値となる。
配列の第一要素がアキュムレータの初期値になる場合は、第二引数は省略できる。

(a,w) => の部分はアロー関数で、書き換えるとfunction(a,w){}。

//100から1-9までの数字を順に引いた結果を出す。
//for文で1-9を足して100から引いた結果を計算し、それと等しければtrueと表示。

const arrMinus = [1,2,3,4,5,6,7,8,9];
const resultR = arrMinus.reduce((a,m) => a -= m, 100);
console.log(resultR);

let count = 0;
for(i = 1; i < 10; i++) count = count + i;
console.log(count);
const resultC = 100 - count;
console.log(resultC);

if(resultR === resultC) console.log(true);
0
3
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
3