Skip to content

Instantly share code, notes, and snippets.

@trezy
Created June 21, 2022 16:02
Show Gist options
  • Save trezy/7339db16aca69b288c51671cb067273e to your computer and use it in GitHub Desktop.
Save trezy/7339db16aca69b288c51671cb067273e to your computer and use it in GitHub Desktop.
Evaluating 2 versions of a JavaScript `array.reduce`
const valueArrays = [
[1, 2],
[100, 400, 700],
]
const TOTAL_COMBINATIONS = valueArrays
.reduce((accumulator, values, index) => {
if (index === 0) {
return values.length
}
return accumulator * values.length
}, 0)
const valueArrays = [
[1, 2],
[100, 400, 700],
]
const TOTAL_COMBINATIONS = valueArrays
.map(values => values.length)
.reduce((accumulator, length) => (accumulatora * length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment