[Solved] Merge objects in an array if they have the same date


This is a good use case for reduce.

const rawData = [
  {
    date: '3/10/2019',
    a: '123',
  },
  {
    date: '3/10/2019',
    b: '456',
  },
  {
    date: '3/11/2019',
    a: '789',
  },
  {
    date: '3/11/2019',
    b: '012',
  },
  {
    date: '3/11/2019',
    c: '345',
  }
];

const groupByDate = array => array.reduce((results, item) => {
  const current = results.find(i => i.date === item.date);
  if (current) {
    for (let property in item) {
      if (property !== 'date') {
        current[property] = item[property];
      }
    }
  } else {
    results.push({...item});
  }
  return results;
}, []);

console.log(groupByDate(rawData));

solved Merge objects in an array if they have the same date