xxxxxxxxxx
var object = arr.reduce((obj, item) => (obj[item.key] = item.value, obj) ,{});
xxxxxxxxxx
var objs = [
{name: "Peter", age: 35},
{name: "John", age: 27},
{name: "Jake", age: 28}
];
objs.reduce(function(accumulator, currentValue) {
return accumulator + currentValue.age;
}, 0); // 35 + 27 + 28 = 90
xxxxxxxxxx
const convertArrayToObject = (array, key) => {
const initialValue = {};
return array.reduce((obj, item) => {
return {
obj,
[item[key]]: item,
};
}, initialValue);
};