Answers for "a array to reduce object data and create new array in javascript"

5

javascript reduce array of objects

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
Posted by: Guest on May-21-2020
0

array of obj to obj with reduce

const convertArrayToObject = (array, key) => {
  const initialValue = {};
  return array.reduce((obj, item) => {
    return {
      ...obj,
      [item[key]]: item,
    };
  }, initialValue);
};
Posted by: Guest on August-26-2021

Code answers related to "a array to reduce object data and create new array in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language