I am trying to loop through array of entries using a for loop and use the reduce method, but the reduce returns after the first entry, never getting to the second, third, etc entries to push the result to an array v.
Any thoughts?
export const groupEventData = eventData => {
console.log(eventData)
// returns[{
// "id": "141",
// "name": "00333",
// "no_transmission": true,
// "low_battery" :true,
// },
// {
// "id": "307",
// "name": "01163",
// "no_transmission": true,
// "low_data_rate": true,
// }
// ]
// now let's group similar event types
let v = [];
let types = ["no_transmission", "low_data_rate", "low_battery"];
for (let x = 0; x <= types.length; x++) {
let data = eventData.reduce((acc, i) => {
if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
acc[i[types[x]]] = [i]
}
else if (Array.isArray(acc[i[types[x]]])) {
acc[i[types[x]]].push(i);
}
else if (typeof acc[i[types[x]]] === 'object') {
acc[i[types[x]]] = [acc[i[types[x]]]]
acc[i[types[x]]].push(i)
}
return acc;
}, {});
// Doesn't add a property for the type if there are no data
Object.keys(data).length && v.push({ [types[x]: data });
return v;
};
}
type,types,eventData, actual/expected output). Thanks.getEventTypes()- is this possible a sync?