[英]lodash filter out objects with null value
我有一個課程對象,我想通過檢查它的類數組是否具有空位置來過濾它。 但是,如果classes數組至少有一個沒有null位置的對象,那么應該返回它。 這是我正在研究的一個示例對象:
courses: [
{
title: "Introduction to English 101",
classes: [{
location: null,
endDate: "2016-03-25T22:00:00.000Z",
startDate: "2016-03-23T22:00:00.000Z",
}]
},
{
title: "Introduction to Japanese 101",
classes: [{
location: {
city: "Paris",
},
endDate: "2016-03-25T22:00:00.000Z",
startDate: "2016-03-23T22:00:00.000Z",
}]
},
{
title: "Introduction to Spanish 101",
classes: [{
location: null,
startDate: "2016-02-23T10:11:35.786Z",
endDate: "2016-02-23T12:11:35.786Z",
},
{
location: {
city: "Montreal",
},
startDate: "2016-04-01T10:11:35.786Z",
endDate: "2016-04-15T10:11:35.786Z",
}],
}
]
這是我期望獲得的結果:
courses: [
{
title: "Introduction to Japanese 101",
classes: [{
location: {
city: "Paris",
},
endDate: "2016-03-25T22:00:00.000Z",
startDate: "2016-03-23T22:00:00.000Z",
}]
},
{
title: "Introduction to Spanish 101",
classes: [{
location: null,
startDate: "2016-02-23T10:11:35.786Z",
endDate: "2016-02-23T12:11:35.786Z",
},
{
location: {
city: "Montreal",
},
startDate: "2016-04-01T10:11:35.786Z",
endDate: "2016-04-15T10:11:35.786Z",
}],
}
]
由於對象的嵌套結構,我無法理解如何過濾這個問題。 任何幫助將不勝感激!
_.filter(obj.courses, function(o) {
return !_.every(o.classes,{ location: null });
});
這個怎么樣:
var data = { courses: [/* your sample data above */] };
var result = data.courses.filter(function(course){
return course.classes && course.classes.some(function(course){
return course && course.location !== null;
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.