Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering Nested Array with Lodash/Javascript

I have the following object array:

var sizeList = [
    { id: 1, title:"Test1",
        type:[{name:"Big", present:false}, {name:"Small", present:true}, {name:"Medium", present:false}]
    },

    { id: 2,title:"Test2",
        type:[{name:"Big", present:false}, {name:"Small", present:true}, {name:"Medium", present:false}]
    },
    { id: 3,title:"Test3",
        type:[{name:"Big", present:false}, {name:"Small", present:true}, {name:"Medium", present:true}]
    }
  ]

I want to filter this list where Medium is True. I currently have this set up.

var specificSizes = _.filter(sizeList.type, { 'name': 'Medium', 'present': true })

This keeps returning an empty array. I also tried this:

       specificSizes = _.filter(sizeList.type, function (type) {
          return _.some(type, {'name': 'Medium', 'present':true})
        });
like image 757
lost9123193 Avatar asked Apr 06 '17 07:04

lost9123193


People also ask

How do you filter nested array of objects in Lodash?

To use filter with nested objects with Lodash, we can filter with the nested property values. const filtered = _. filter(summary. data, ["category.

What does Lodash filter return?

Lodash's `filter()` Function The function you pass to filter() is called the predicate. If the predicate returns a falsy value (like null , undefined , 0 , or '' ), Lodash filters that value out.


1 Answers

With lodash, you could wrap the condition in the same structure for the test, as the original object.

_.filter(sizeList, { type: [{ name: 'Medium', present: true }] })

var sizeList = [{ id: 1, title: "Test1", type: [{ name: "Big", present: false }, { name: "Small", present: true }, { name: "Medium", present: false }] }, { id: 2, title: "Test2", type: [{ name: "Big", present: false }, { name: "Small", present: true }, { name: "Medium", present: false }] }, { id: 3, title: "Test3", type: [{ name: "Big", present: false }, { name: "Small", present: true }, { name: "Medium", present: true }] }],      
    result = _.filter(sizeList, { type: [{ name: 'Medium', present: true }] });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

In plain Javascript, you could use Array#filter for the outer array and check with Array#some if one condition met.

var sizeList = [{ id: 1, title: "Test1", type: [{ name: "Big", present: false }, { name: "Small", present: true }, { name: "Medium", present: false }] }, { id: 2, title: "Test2", type: [{ name: "Big", present: false }, { name: "Small", present: true }, { name: "Medium", present: false }] }, { id: 3, title: "Test3", type: [{ name: "Big", present: false }, { name: "Small", present: true }, { name: "Medium", present: true }] }],      
    result = sizeList.filter(function (a) {
        return a.type.some(function (b) {
            return b.name === 'Medium' && b.present;
        });
    });
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 51
Nina Scholz Avatar answered Sep 28 '22 00:09

Nina Scholz