2015-10-02 33 views
0

我正在使用Node.js,这是我对路线的回应。如何通过其值删除数组中的对象?

enter image description here

我想REMOVE具有的"existence":false和输出具有的“存在”的值对象的值对象:真正的”

这是到目前为止我的代码

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)+')'); 
    } 
}); 

回答

2

您可以使用Array.prototype.filter

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

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

你好@Buzinas。我想删除具有“存在”值的对象:false“'并输出值为”存在“的对象:true” – Agent69

+0

@ Agent69这正是我在这里所做的; – Buzinas

+0

If你不喜欢可读性,你可以将'filter'返回到'return c.existence === true;'。但由于它已经是一个布尔值,所以绝对没有必要这样做。 – Buzinas

相关问题