Q:

Removing duplicate objects in object array while keeping unique string values

Please bear with me, I'm a novice javascript developer with a sorting problem that I can't wrap my head around.  
I'm retrieving an array of objects (JSON response) by an api like this:
var arr = [
    {
        "year": "2011",
        "activity": "bowling"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2011",
        "activity": "cooking"
    },
    {
        "year": "2006",
        "activity": "singing"
    }
]

I have a function that removes all of the duplicate years (as intended), but I wish to take the 'activity' value of the duplicate object and add it to the activity value of that same year is left after removing duplicates
My function that removes the duplicate year is as follows
var new_arr = new_arr.reduce((unique, o) => {
  if(!unique.some(obj => obj.year === o.year)) {
    unique.push(o);
  }
  return unique;
},[]);

which leaves me with this
var arr = [
    {
        "year": "2011",
        "activity": "bowling"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2006",
        "activity": "singing"
    }
]

What I wish to accomplish:
var arr = [
    {
        "year": "2011",
        "activity": "bowling, cooking"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2006",
        "activity": "singing"
    }
]

JSFiddle: https://jsfiddle.net/4s1uaex0/2/
I feel like I'm almost there, but I don't know where to start from here.  Can anyone point me into the right direction with a guide or example?  Help would be greatly appreciated!  Thank you

A:

Try reduce-ing the activity to an array in the accumulator first, then join each activity property by commas afterwards.  You can achieve this with a single reduce, which is O(N), a some inside of it will make it O(N^2), which is not necessary and probably undesirable:

var arr = [
    {
        "year": "2011",
        "activity": "bowling"
    },
    {
        "year": "2009",
        "activity": "shopping",
    },
    {
        "year": "2011",
        "activity": "cooking"
    },
    {
        "year": "2006",
        "activity": "singing"
    }
];
const output = Object.values(
  arr.reduce((a, { year, activity }) => {
    if (! a[year]) a[year] = { year, activity: [] };
    a[year].activity.push(activity);
    return a;
  }, {})
);
output.forEach((item) => {
  item.activity = item.activity.join(', ');
});
console.log(output);

