Last active
September 15, 2016 14:18
-
-
Save suneo3476/0b6d5f024ed53951c83d75f48f2d6cce to your computer and use it in GitHub Desktop.
私と Array.prototype.reduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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