IS
Size: a a a
IS
S
j
j
ВА
IS
const source = JSON.parse("{\"1\":{\"profile_id\":1,\"group\":\"first\",\"full_name\":\"value 1\",\"price\":1700},\"2\":{\"profile_id\":2,\"group\":\"first\",\"full_name\":\"value 2\",\"price\":1800},\"3\":{\"profile_id\":3,\"group\":\"second\",\"full_name\":\"value 3\",\"price\":2100},\"4\":{\"profile_id\":4,\"group\":\"third\",\"full_name\":\"value 4\",\"price\":1500},\"5\":{\"profile_id\":5,\"group\":\"second\",\"full_name\":\"value 5\",\"price\":3200},\"6\":{\"profile_id\":6,\"full_name\":\"value 6\",\"price\":\"1900\"},\"7\":{\"profile_id\":7,\"group\":\"second\",\"full_name\":\"value 7\",\"price\":2000},\"8\":{\"profile_id\":8,\"group\":\"third\",\"full_name\":\"value 8\",\"price\":2200},\"9\":{\"profile_id\":9,\"group\":\"first\",\"full_name\":\"value 9\",\"price\":3000},\"10\":{\"profile_id\":10,\"full_name\":\"value 10\",\"price\":2800}}")всё, вот так. те, что без группы, летят в помойку
const aggregated = Object.entries(source).reduce((acc, [key, element]) => {
const { group, price } = element;
if (!group) {
return acc;
}
if (!acc.grouped.hasOwnProperty(group)) {
acc.grouped[group] = {};
}
acc.grouped[group][key] = element;
if (!acc.minPrice || acc.minPrice > price) {
acc.minPrice = price;
}
if (!acc.maxPrice || acc.maxPrice < price) {
acc.maxPrice = price;
}
return acc;
}, {
grouped: {},
minPrice: null,
maxPrice: null
});
console.log(aggregated);
IS
IS
N
S
BB
BB
BB
N
BB
BB
N
АД
АД
АД