Lodash: how do I use filter when I have nested Object?

JavascriptLodash

Javascript Problem Overview


Consider this example. I am using Lodash

 'data': [
        {
            'category': {
                'uri': '/categories/0b092e7c-4d2c-4eba-8c4e-80937c9e483d',
                'parent': 'Food',
                'name': 'Costco'
            },
            'amount': '15.0',
            'debit': true
        },
        {
            'category': {
                'uri': '/categories/d6c10cd2-e285-4829-ad8d-c1dc1fdeea2e',
                'parent': 'Food',
                'name': 'India Bazaar'
            },
            'amount': '10.0',
            'debit': true
        },
        {
            'category': {
                'uri': '/categories/d6c10cd2-e285-4829-ad8d-c1dc1fdeea2e',
                'parent': 'Food',
                'name': 'Sprouts'
            },
            'amount': '11.1',
            'debit': true
        },

When I do

_.filter(summary.data, {'debit': true})

I get all the objects back.

what I want?

I want all the objects where category.parent == 'Food', how can I do that?

I tried

_.filter(summary.data, {'category.parent': 'Food'})

and got

[]

Javascript Solutions


Solution 1 - Javascript

lodash allows nested object definitions:

_.filter(summary.data, {category: {parent: 'Food'}});

As of v3.7.0, lodash also allows specifying object keys in strings:

_.filter(summary.data, ['category.parent', 'Food']);

Example code in JSFiddle: https://jsfiddle.net/6qLze9ub/

lodash also supports nesting with arrays; if you want to filter on one of the array items (for example, if category is an array):

_.filter(summary.data, {category: [{parent: 'Food'}] }); 

If you really need some custom comparison, that's when to pass a function:

_.filter(summary.data, function(item) {
  return _.includes(otherArray, item.category.parent);
});

Solution 2 - Javascript

_.filter(summary.data, function(item){
  return item.category.parent === 'Food';
});

Solution 3 - Javascript

beginning from v3.7.0 you can do it in this way:

_.filter(summary.data, 'category.parent', 'Food')

Solution 4 - Javascript

_.where(summary.data, {category: {parent: 'Food'}});

Should do the trick too

Solution 5 - Javascript

In lodash 4.x, you need to do:

_.filter(summary.data, ['category.parent', 'Food'])

(note the array wrapping around the second argument).

This is equivalent to calling:

_.filter(summary.data, _.matchesProperty('category.parent', 'Food'))

Here are the docs for _.matchesProperty:

// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondaydreamerView Question on Stackoverflow
Solution 1 - JavascriptAkrikosView Answer on Stackoverflow
Solution 2 - JavascriptidbeholdView Answer on Stackoverflow
Solution 3 - JavascripteviliveView Answer on Stackoverflow
Solution 4 - JavascriptDenisView Answer on Stackoverflow
Solution 5 - JavascriptiamyojimboView Answer on Stackoverflow