I have an array of objects with the following structure that is being sent as response:
let sampleData = [
{ values: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 },
{ values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},
{ time: "14354545454", sum: 0},
{ time: "14354545454", sum: 0} }
];
I need to take each key within each object inside array and form an array out of it. Basically grouping based on the key present in all the objects.If the object has no "values" it should return 0 across the val1,val2,val3.
But the issue , the order of the keys "high,low,medium" inside "values" object in the question is unpredictable. With whatever code I have written keys that are present in the first object will be grouped and follows that order. But I want be it any input order, It should always give me this order "High Medium Low" Order.
The resultant array of objects should look like and should always follow "High Medium Order":
result = [
{ name: 'High', data: [4, 5, 0, 0] },
{ name: 'Medium', data: [5, 3, 0, 0] },
{ name: 'Low', data: [7, 1, 0, 0] }
]
I have tried the following:
let sampleData = [{ values: { High: 4, Low: 5, Medium: 7 } , time: "1571372233234" , sum: 16 },{ values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},{ time: "14354545454", sum: 0},{ time: "14354545454", sum: 0 }];
const keySet = new Set(sampleData.flatMap(a => Object.keys(a.values || {})));
const merged = sampleData.reduce((acc, {
values = {}
}) => {
keySet.forEach(name => {
acc[name] = acc[name] || {
name,
data: []
};
acc[name].data.push(values[name] || 0);
});
return acc;
}, {});
console.log(merged);
ecmascript-5? Especially if you are using ES6 syntax