Skip to content

Instantly share code, notes, and snippets.

@suneo3476
Last active September 15, 2016 14:18
Show Gist options
  • Save suneo3476/0b6d5f024ed53951c83d75f48f2d6cce to your computer and use it in GitHub Desktop.
Save suneo3476/0b6d5f024ed53951c83d75f48f2d6cce to your computer and use it in GitHub Desktop.
私と Array.prototype.reduce
//reduce第二引数なし -> 初期値は [1,3,5] の 添字0の要素である 1
const sum1 = [1,3,5].reduce(function(pre, current){
console.log(pre, current)
return pre + current*2
})
console.log(sum1) // 1 + 3*2 + 5*2 = 17
//reduce第二引数あり -> 初期値は 指定した 0
const sum2 = [1,3,5].reduce(function(pre, current){
console.log(pre, current)
return pre + current*2
},0) // <- ここ
console.log(sum2) // 0 + 1*2 + 3*2 + 5*2 = 18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment