var initialValue = 0;
var sum = [{x: 1}, {x:2}, {x:3}].reduce((accumulator, currentValue) => {
return accumulator + currentValue.x;
}, 0)
console.log(sum) // logs 6
var flattened = [[0, 1], [2, 3], [4, 5]].reduce((acc, cur) => acc.concat(cur),
[]
);
console.log(flattened)
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
var countedNames = names.reduce(function (allNames, name) {
if (name in allNames) {
allNames[name]++;
}
else {
allNames[name] = 1;
}
return allNames;
}, {});
console.log(countedNames)
var people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 }
];
function groupBy(arr, props) {
return arr.reduce(function (acc, obj) {
var key = obj[props];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {});
}
var groupedPeople = groupBy(people, 'age');
console.log(groupedPeople)
console