cup_of
cup_of

Reputation: 6697

Remove object from array with a 0 value

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?

Upvotes: 1

Views: 90

Answers (5)

Aravinda Meewalaarachchi
Aravinda Meewalaarachchi

Reputation: 2639

It is always best to create a predicated if statement to check the validation before the business logic execute. Here is my solution to solve this problem.

  modifyFatalitiesByCause(data) {
    let array = [];
    for (let record in data) {
     if(isDeathCountZero(record, data)){
       array.push({
         "name": data[record]['HarmfulEvent'],
         "value": parseInt(data[record]['Deaths']),
       });
     } 
   }
    return array;
  }

 isDeathCountZero(record, data){
    return (parseInt(data[record]['Deaths']) != 0);
}

Upvotes: 1

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38552

Yes you can do a check before pushing element on array like this if(parseInt(data[x]['Deaths']) > 0 and then push non-zero values but Array.prototype.filter() seems cool to me :)

let array = [{
    name: "Injured in Vehicle (Non-Collision)",
    value: 1
  },
  {
    name: "Fire Hydrant",
    value: 1
  },
  {
    name: "Snow Bank",
    value: 0
  },
  {
    name: "Cargo/Equipment",
    value: 0
  }
];
result = array.filter(elm => elm.value > 0);
console.log(result);

Upvotes: 3

amrender singh
amrender singh

Reputation: 8239

You can simply add a check in your for loop, Add objects in your array, whose data[x]['Deaths']) are non-zero. Try the following:

for (let x in data) {
     if(parseInt(data[x]['Deaths']) != 0){
       array.push({
         "name": data[x]['HarmfulEvent'],
         "value": parseInt(data[x]['Deaths']),
       })
     } 
}

Upvotes: 1

M. Wyatt
M. Wyatt

Reputation: 170

You could remove them all at the end but it would be more efficient to just not push them to your array while you're building it. Something like this:

if (parseInt(data[x]['Deaths']) { // 0 is falsey
  array.push({
    "name": data[x]['HarmfulEvent'],
    "value": parseInt(data[x]['Deaths']),
  });
}

Upvotes: 2

Gian Tomakin
Gian Tomakin

Reputation: 193

try to use array filter to filter the results. refrence: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Upvotes: -1

Related Questions