js sum of array
[1, 2, 3, 4].reduce((a, b) => a + b, 0) // Output: 10
Source: stackoverflow.com
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]
Source: developer.mozilla.org
js reduce a array of straing
var authors = [{name: 'some author'},{name: 'another author'},{name: 'last author'}] var authorString = authors.map(function(author){ return author.name; }).join(","); console.log(authorString);
Source: stackoverflow.com
javascript sum of array
const sum = arr => arr.reduce((a, b) => a + b, 0);
reduce in javascript
// Reduce in javascript advance // output: { '22': 2, '24': 2, '26': 1 } const user = [ {name:"Abhishek",age:24}, {name:"Dhruval",age:22}, {name:"Anish",age:26}, {name:"Aakash",age:22}, {name:"Darshil",age:24}, ] const newUser = user.reduce((acc,cur)=>{ if(acc[cur.age]){ acc[cur.age] = acc[cur.age] +1 } else{ acc[cur.age] = 1 } return acc; },{}) console.log(newUser)
.reduce javascript
reduce function sample solution