how to flatten array with reduce in javascript
let flattened = [[0, 1], [2, 3], [4, 5]].reduce(
function(accumulator, currentValue) {
return accumulator.concat(currentValue)
},
[]
)
// flattened is [0, 1, 2, 3, 4, 5]
//note idx and sourceArray are optional
const sum = array.reduce((accumulator, element[, idx[, sourceArray]]) => {
//arbitrary example of why idx might be needed
return accumulator + idx * 2 + element
}, 0);
Also in JavaScript:
- html set textarea value
- javascript .tolowercase
- find number in array js
- call local function javascript
- purecomponent re rendering
- change array index position in javascript by up and down click
- looping in javascript
- how to create json file in c#
- how can we take selected index value of dropdown in javascript
- get uislider
- generate random number jquery
- javascript getelementbyid
- access index of array javascript
- display amount with currency for jquery
- javascript after dom ready
- javascript substring vs substr
- algolia react hits
- javascript if contains character
- instanceof javascript
- angular 6 key value pair getvalue example
- sorting array from highest to lowest javascript
- js detect right key click
- nodejs print
- call method in javascript