I am generating an array with objects from some outside data. Here is my code to do so:
modifyFatalitiesByCause(data) {
let array = [];
for (let x in data) {
array.push({
"name": data[x]['HarmfulEvent'],
"value": parseInt(data[x]['Deaths']),
})
}
return array;
}
This works fine and will output this data (here's just a small chunk of a big array):
[
{name: "Injured in Vehicle (Non-Collision)", value: 1},
{name: "Fire Hydrant", value: 1},
{name: "Snow Bank", value: 0},
{name: "Cargo/Equipment", value: 0}
]
I would like to not have the objects with a value of 0 appear in my array. Will I need to remove these at the end or can I modify my for x in data loop to only push the objects that don't have a value of 0?
Which is the easier solution?
array.filter(x => x.value)? or simply add anifcondition before youpushto check thatdata[x]['Deaths'] !== 0