Javascript 从多个参数筛选嵌套对象
我希望能够搜索具有嵌套对象的数组中的所有值。搜索参数不应该是字符串,而应该是如下字符串的数组:['1970','Comicle','family'] 我如何用lodash解决这个问题?我已经尝试了一段时间,但没有解决这个问题 测试数据:Javascript 从多个参数筛选嵌套对象,javascript,lodash,Javascript,Lodash,我希望能够搜索具有嵌套对象的数组中的所有值。搜索参数不应该是字符串,而应该是如下字符串的数组:['1970','Comicle','family'] 我如何用lodash解决这个问题?我已经尝试了一段时间,但没有解决这个问题 测试数据: var movies = [ { id: 1, title: '22 Jump Street', category: 'Comedy', description: 'After makin
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']));
您是否正在尝试按类别“拉”所有与数组中的geners匹配的影片?我正在尝试收集与给定的所有参数匹配的对象。比如,如果我给搜索参数['family','Comicle']没有找到,但是如果我给['1970','is']标题为'Maleficent'和'How to Train Your Dragon 2'的对象将被返回/显示。如果你愿意跳过搜索词“id”,“title”和其他键,你可以
JSON.stringify()
每个对象,然后测试每个搜索词的索引。。如果这不适合你-O(n*m)-你可能需要为每个对象创建一个索引,而不是这实际上是一个好主意deathApril:)我需要一些调整,但我喜欢这种方法。谢谢:)