2015-10-02 30 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

相關問題