164 views
Creating an aggregated product table
Task condition
Given an array of objects representing products, implement a function that collects the products into a table-like structure, grouping them by the type field and summing the total weight of each group.
const products = [
{ id: 'ab', name: 'Name-01', type: 'steel', weight: 1 },
{ id: 'bc', name: 'Name-02', type: 'cast iron', weight: 2 },
{ id: 'cd', name: 'Name-03', type: 'steel', weight: 3 },
{ id: 'de', name: 'Name-04', type: 'cast iron', weight: 4 },
{ id: 'ef', name: 'Name-05', type: 'cast iron', weight: 5 },
{ id: 'fg', name: 'Name-06', type: 'steel', weight: '4' },
];
// The desired result is an object where the key is the type, and the value is an object with an array of ids and the total weight:
// {
// 'steel': { ids: ['ab', 'cd', 'fg'], totalWeight: 8 },
// 'cast iron': { ids: ['bc', 'de', 'ef'], totalWeight: 11 },
// }
const generateTable = arr => {
console.log(arr);
};