Home > front end >  Merge Objects in a array and increase count
Merge Objects in a array and increase count

Time:12-08

[{name:"abc",value:5},{name:"abc",value:10},{name:"abc1",value:5},{name:"abc1",value:15}]

I want to merge it by name so that the new array will be

[{name:"abc",value:15},{name:"abc1",value:20}]

Can i do it with es6 or a simple function

CodePudding user response:

Using reduce and without find or findIndex

const data = [{name:"abc",value:5},{name:"abc",value:10},{name:"abc1",value:5},{name:"abc1",value:15}];

const summedDataObj = data.reduce((acc, entry) => {
  if (acc[entry.name]) acc[entry.name].value  = entry.value;
  else acc[entry.name] = entry;
  return acc;
}, {});

const summedDataArr = Object.values(summedDataObj);

console.log(summedDataArr);

CodePudding user response:

We can do it via Array.reduce()

let data = [{name:"abc",value:5},{name:"abc",value:10},{name:"abc1",value:5},{name:"abc1",value:15}]

let result = data.reduce((a,{name,value}) => {
  let obj = a.find(e => e.name === name)
  if(obj){
   obj.value  = value
  }else{
   a.push({name,value})
  }
  return a
},[])
console.log(result)

CodePudding user response:

you can group the data by name using this function:

function groupBy(arr, prop) {
    const map = new Map(Array.from(arr, obj => [obj[prop], []]));
    arr.forEach(obj => map.get(obj[prop]).push(obj));
    return Array.from(map.values());
}

this yields this result:

[
    [
        {"name": "abc", "value": 5},
        {"name": "abc", "value": 10}
    ],
    [
        {"name": "abc1", "value": 5},
        {"name": "abc1", "value": 15}
    ]
]

which can be aggregated by using reduce on each resulting array:

groupedData.map(entry=>entry.reduce((acc,cur)=>({
    ...acc,
    value: acc.value   cur.value
})))

so all together we get:

function groupBy(arr, prop) {
    const map = new Map(Array.from(arr, obj => [obj[prop], []]));
    arr.forEach(obj => map.get(obj[prop]).push(obj));
    return Array.from(map.values());
}

const data = [{name:"abc",value:5},{name:"abc",value:10},{name:"abc1",value:5},{name:"abc1",value:15}]

const aggregatedData = groupBy(data,"name")
    .map(entry=>entry.reduce((acc,cur)=>({
        ...acc,
        value:acc.value cur.value
    })))

CodePudding user response:

const obj = [{name:"abc",value:5},{name:"abc",value:10},{name:"abc1",value:5},{name:"abc1",value:15}]

arr = obj.reduce((obj, item) => {  
    let find = obj.find(i => i.name === item.name && i.date === item.date);  
    let _d = {  
        ...item
    }
    find ? (find.value  = item.value ) : obj.push(_d);
    return obj;
}, [])

console.log(arr);

CodePudding user response:

const data = [{
    name: "abc",
    value: 5
  },
  {
    name: "abc",
    value: 10
  },
  {
    name: "abc1",
    value: 5
  },
  {
    name: "abc1",
    value: 15
  },
];

const groupArr = data.reduce((r, a) => {
  const idx = r.findIndex((el) => el.name === a.name);
  idx === -1 ? r.push(a) : (r[idx].value  = a.value);
  return r;
}, []);

console.log(groupArr);

CodePudding user response:

You can achieve this with the help of Array#reduce method.

Live Demo :

const arr = [{name:"abc",value:5},{name:"abc",value:10},{name:"abc1",value:5},{name:"abc1",value:15}];

const res = arr.reduce((obj, curr) => {
    if (obj.hasOwnProperty(curr.name) && obj[curr.name].name === curr.name) {
    obj[curr.name].value  = curr.value
  } else {
    obj[curr.name] = curr;
  }
  return obj
}, {});

console.log(Object.values(res));

  • Related