从多个参数中筛选嵌套对象
Filtering nested object from multiple parameres
我希望能够搜索具有嵌套对象的数组中的所有值。搜索参数不应该是字符串,而是像这样的字符串数组:['1970','comedy','family']
我如何用lodash解决这个问题?我试了一段时间也没能解决这个问题。
测试数据:var movies = [
{
id: 1,
title: '22 Jump Street',
category: 'Comedy',
description: 'After making their way through high school (twice), big changes are in store for officers Schmidt and Jenko when they go deep undercover at a local college',
director: {
name: 'Phil',
lastName: 'Lord',
dob: '12-07-1975'
},
},
{
id: 2,
title: 'How to Train Your Dragon 2',
category: 'Animation',
description: 'When Hiccup and Toothless discover an ice cave that is home to hundreds of new wild dragons and the mysterious Dragon Rider, the two friends find themselves at the center of a battle to protect the peace.',
director: {
name: 'Dean',
lastName: 'DeBlois',
dob: '07-06-1970'
},
},
{
id: 3,
title: 'Maleficent',
category: 'Family',
description: 'A vengeful fairy is driven to curse an infant princess, only to discover that the child may be the one person who can restore peace to their troubled land.',
director: {
name: 'Robert',
lastName: 'Stromberg',
dob: '22-08-1970'
},
}
];
@Aprillion评论的扩展答案:
function filterMovies(query) {
var params = Array.prototype.slice.call(query);
var result = movies;
params.forEach(function(param) {
result = result.filter(function(movie) {
return JSON.stringify(movie).indexOf(param) >= 0;
});
});
return result;
}
console.log('filtered: ', filterMovies(['1970', 'Robert']));
相关文章:
- 用嵌套函数和默认函数定义函数
- 如何在ReactJS JSX中执行嵌套的if-else语句
- AngularJS-如何在嵌套ng重复中对已筛选的孙项求和
- 从mongo返回的对象(findOne)中筛选/返回单个嵌套对象
- 如果图元属于嵌套图元,请选择对其进行筛选的图元
- 筛选客户端上的嵌套对象
- 筛选嵌套对象数组
- 筛选嵌套 JSON 中的数据
- 在 ngRepeat 中筛选嵌套属性
- 筛选器深层嵌套属性返回空数组
- 使用文本输入筛选 LI - 嵌套 LI 不显示
- 如何在 Ember 应用中实现嵌套全局筛选器
- 筛选器嵌套在 Jade 中失败
- Angularjs嵌套ng重复,在筛选器中使用表达式
- 使用forEach、映射或筛选器克隆和操作嵌套对象,而不修改原始对象
- 从多个参数中筛选嵌套对象
- 使用lodash按键筛选嵌套对象
- 嵌套映射和筛选方法
- 如何使AngularJS在嵌套属性上进行筛选
- 数组数组中的Javascript嵌套筛选器