Filtering nested object from multiple parameres

I wish to be able to search for all values in an array with nested objects. The search param should not be a string, but an array of strings like this: ['1970','comedy','family']

how would I solve this with lodash? I've been trying for some time without being able to solve this.

Test data:

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'
        },

    }
];

Upvotes: 0

Views: 110

Answers (1)

Krzysztof Safjanowski
Krzysztof Safjanowski

Reputation: 7438

Extended answer for @Aprillion comments:

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']));

Upvotes: 1

Related Questions