const arr = [
{id: 1, groupId: 100, source: 1},
{id: 2, groupId: 100, source: 2},
{id: 3, groupId: 100, source: 1},
{id: 4, groupId: 200, source: 1},
{id: 5, groupId: 200, source: 0},
{id: 6, groupId: 300, source: 1},
{id: 7, groupId: 300, source: 0},
{id: 8, groupId: 300, source: 1},
{id: 9, groupId: 400, source: 1},
{id: 10, groupId: 400, source: 0},
{id: 11, groupId: 400, source: 0},
{id: 12, groupId: 500, source: 2},
{id: 13, groupId: 500, source: 1},
];
console.log(getSortBy() );//{id: 5, groupId: 200, source: 0},
//если массив меняем
const arr = [
{id: 1, groupId: 100, source: 1},
{id: 2, groupId: 100, source: 2},
{id: 3, groupId: 100, source: 1},
{id: 4, groupId: 200, source: 1},
{id: 5, groupId: 200, source: 2},
{id: 6, groupId: 300, source: 1},
{id: 7, groupId: 300, source: 0},
{id: 8, groupId: 300, source: 1},
{id: 9, groupId: 400, source: 1},
{id: 10, groupId: 400, source: 2},
{id: 11, groupId: 400, source: 2},
{id: 12, groupId: 500, source: 2},
{id: 13, groupId: 500, source: 1},
];
console.log(getSortBy() );//{id: 7, groupId: 300, source: 0},
const arr = [
{id: 1, groupId: 100, source: 1},
{id: 2, groupId: 100, source: 2},
{id: 3, groupId: 100, source: 1},
{id: 4, groupId: 200, source: 1},
{id: 5, groupId: 200, source: 0},
{id: 6, groupId: 300, source: 1},
{id: 7, groupId: 300, source: 0},
{id: 8, groupId: 300, source: 1},
{id: 9, groupId: 400, source: 1},
{id: 10, groupId: 400, source: 0},
{id: 11, groupId: 400, source: 0},
{id: 12, groupId: 500, source: 2},
{id: 13, groupId: 500, source: 1},
];
const result = arr.reduce((obj, item) => {
if (obj[item.groupId]) {
obj[item.groupId]['source'].push(item.source);
} else {
obj[item.groupId] = {
source: [item.source]
}
}
return obj;
}, {})
console.log(result)