lodash 过滤掉空值对象
lodash filter out objects with null value
我有一个课程对象,我想通过检查它的 类 数组是否有空位置来过滤它。但是,如果 类 数组至少有一个对象没有空位置,那么它应该被返回。这是我正在处理的示例对象:
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 and _.every 的组合怎么样:
_.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;
});
});
我有一个课程对象,我想通过检查它的 类 数组是否有空位置来过滤它。但是,如果 类 数组至少有一个对象没有空位置,那么它应该被返回。这是我正在处理的示例对象:
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 and _.every 的组合怎么样:
_.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;
});
});