7

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})
        });
2
  • the title is about "sorting"? what should be sorted? Commented Apr 6, 2017 at 7:11
  • @RomanPerekhrest Ah my bad, I meant filtering. I will update that now Commented Apr 6, 2017 at 7:12

1 Answer 1

16

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; }

Sign up to request clarification or add additional context in comments.

2 Comments

This is extremely useful. Thanks a lot!
Hi @NinaScholz, can you please check my post stackoverflow.com/questions/78419788/…, I have a deep nested object and I tried filter + nested some but I still could not get the right result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.