Last active
March 19, 2020 01:05
-
-
Save masatomix/b8b2a388f10f5862ace9bd72fff234e9 to your computer and use it in GitHub Desktop.
Reduceのサンプル. 収集するaccumulatorと初期値を設定するのが重要
This file contains hidden or 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
const array1 = [1, 2, 3, 4,5]; | |
const reducer = (accumulator, currentValue,index) => { | |
console.log(`---`) | |
console.log(`Index: ${index}`) | |
console.log(`A: ${accumulator}`) | |
console.log(`C: ${currentValue}`) | |
return {value: accumulator.value + currentValue} | |
return accumulator+currentValue | |
} | |
// 1 + 2 + 3 + 4 | |
console.log(JSON.stringify(array1.reduce(reducer,{value:0}))); | |
// expected output: 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment