const data = [
{id: 1, color: red},
{id: 1, color: black},
{id: 2, color: green},
{id: 3, color: white},
{id: 3, color: blue}
...
]
const data = [
{id: 1, colors: [red, black]},
{id: 2, color: green},
{id: 3, color: [white, blue]},
]
const group = (arr, idKey, valKey) =>
Object.values(arr.reduce((acc, { [idKey]: id, [valKey]: val }) => (
(acc[id] ??= { [idKey]: id, [valKey]: [] })[valKey].push(val),
acc
), {}));
const result = group(data, 'id', 'color');