Agent69
Agent69

Reputation: 678

How to remove objects in an array by its value?

I am using Node.js and this is my response of my routes.

enter image description here

I want to REMOVE the objects that have the value of "existence":false and output the object that has value of "existence":true"

This is my code so far.

schedule.get('/conference/schedule_participants/:circle/:confUid/:schedId', function(req, res) {
  if(req.schedId){
    getParticipants( req.params, function(contacts){
      results.contacts=contacts;
      res.send('response('+JSON.stringify(results.contacts)+')');
    }); 
  } else{
      res.send('response('+JSON.stringify(results.contacts)+')');
  }
});

Upvotes: 0

Views: 41

Answers (2)

Buzinas
Buzinas

Reputation: 11725

You can use Array.prototype.filter:

var filtered = results.contacts.filter(function(c) {
  return c.existence;
});

res.send('response(' +JSON.stringify(filtered) + ')');

Upvotes: 2

Lucius
Lucius

Reputation: 2872

Use Array.prototype.filter()

var filtered = results.contacts.filter(function(contact) {
    return contact.existence;
});

Upvotes: 1

Related Questions