So, you want to flatten this array:
const arr = [
{ id: 1, nickName: "greatest1337" },
{ id: 1, nickName: "Bob" },
{ id: 2, nickName: "notReallyCool" },
];
into this array:
[
{ id: 1, nickName: ["greatest1337", "Bob"] },
{ id: 2, nickName: ["notReallyCool"] },
];
First, as the desired result is an array, the initial value of the accumulator must be an empty array:
arr.reduce((newArr, currentObj) => {
}, []);
Next, we need to check, with the filter method, if our accumulator, the newArr, contains an object with the same id as currentObj. For the sake of clarity, let’s use object destructuring on currentObj to get its properties:
arr.reduce((newArr, { id, nickName }) => {
let objWithSameId = newArr.filter((obj) => obj.id === id);
}
}, []);
objectWithSameId will be an empty array, if the current object is the first with its id processed. Otherwise, objectWithSameId will be an array with only one object.
So, we check its length. If it is 0, we return newArr concatenated with a new object. Otherwise, we push the new nickname to the existing object:
arr.reduce((newArr, { id, nickName }) => {
let objWithSameId = newArr.filter((obj) => obj.id === id);
if (objWithSameId.length === 0) {
return newArr.concat({
id,
nickName: [nickName],
});
} else {
objWithSameId[0].nickName.push(nickName);
return newArr;
}
}, []);
I won’t lie, I had a headache trying to solve this!!! 
