The sum of keys in an array of objects for each day

The issue with your code is the following part in your if-statement:

[curr.source].length + 1

Here you’re creating an array with curr.source (1 element), grabbing its length which will always be 1, and then adding 1 to it, giving you 2. Instead, you can change this line to be:

(node[curr.source] ?? 0) + 1

This will grab the source’s value from your found node, and it will add one to the current value. If the value hasn’t been set on the node, it will default to 0 by using ?? 0 (here ?? is the Nullish coalescing operator – it could be replaced with || if you need better browser support). I would also suggest defaulting your node value to an empty object if it isn’t found when using .find() by using ?? {}, which will allow you to remove your inner if-statement and run that portion of code each time:

const input = [ { date: "01-01-2021", source: "TT", }, { date: "01-01-2021", source: "TT", }, { date: "02-01-2021", source: "FB", }, { date: "02-01-2021", source: "TT", }, ];

const result = input.reduce((acc, curr) => {
  const node = acc.find((i) => i.date === curr.date) ?? {};
  return [...acc.filter((i) => i.date !== curr.date), { ...node, date: curr.date, [curr.source]: (node[curr.source] ?? 0) + 1 }];
}, []);

console.log(result);

Another option that would be more efficient is to build a Map/object that is keyed by your date key from your objects. For each iteration of your reduce method you can get the current accumulated object at your current date from the Map, and update the source count based on the current source. You can then transform the Map into an array of your accumulated objects by using Array.from() on that Map’s .values() iterator:

const input = [ { date: "01-01-2021", source: "TT", }, { date: "01-01-2021", source: "TT", }, { date: "02-01-2021", source: "FB", }, { date: "02-01-2021", source: "TT", }, ];

const res = Array.from(input.reduce((acc, {date, source}) => {
  const seen = acc.get(date) ?? {};
  return acc.set(date, {...seen, date, [source]: (seen[source] ?? 0) + 1});
}, new Map).values());
console.log(res);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top