Underscore.js : findWhere with nested property value

findwhere lodash
npm underscore
lodash find nested object
underscore remove object from array
javascript find value in nested object
underscore pluck nested
get value from nested json object in javascript
underscore replace object in array

How do I go about the filtering below:

[{
    "id": 100,
    "title": "Tlt1",
    "tax": [{
        "name": "Tax1",
        "id": 15
    }, {
        "name": "Tax1",
        "id": 17
    }]
}, {
    "id": 101,
    "title": "Tlt2",
    "tax": [{
        "name": "Tax2",
        "id": 16
    }]
}, {
    "id": 102,
    "title": "Tlt3",
    "tax": [{
        "name": "Tax3",
        "id": 17
    }, {
        "name": "Tax3",
        "id": 18
    }]
}]

to get only those where tax.id is 17, as per below:

[
    {
        "id": 100,
        "title": "Tlt1",
        "tax": [
            {
                "name": "Tax1",
                "id": 15
            },
            {
                "name": "Tax1",
                "id": 17
            }
        ]
    },
    {
        "id": 102,
        "title": "Tlt3",
        "tax": [
            {
                "name": "Tax3",
                "id": 17
            },
            {
                "name": "Tax3",
                "id": 18
            }
        ]
    }
]

Currently I use the method below, but maybe there is more clean way of going about this?

var arr = [];
_(data).each(function (v1, k1) {
    _(v1.tax).each(function (v2, k2) {
        if (v2.id == id) {
            arr.push(v1);
        }
    });
});

Demo here: http://jsfiddle.net/7gcCz/2/

Any suggestion much appreciated.

You may use the combination of _.filter and _.where

_.filter(data, function(obj) {
    return _.where(obj.tax, {id: ID_TO_FIND}).length > 0;
})

See demo: http://jsfiddle.net/hCVxp/

Update

Thanks to @GruffBunny. A more efficient way is to use _.some to avoid looping through all tax items:

var arr = _.filter(data, function(obj) {
    return _.some(obj.tax, {id: ID_TO_FIND});
});

See demo: http://jsbin.com/putefarade/1/edit?html,js,console

Underscore.js, Property: This parameter contains the test condition. Return value: The details of the element selected from the list are returned. Only the first element matched will​  See Your Home Value in Today's Market. Pull Quick and Easy Estimates Today! Before You Sell, Get 5 Free Estimates from the Top Data Sources Online.

Use _.filter to find candidate items and _.some to check for the existence of an item in a collection:

var filteredList = _.filter(list, function(item){
    return _.some(item.tax, { id: 17 });
});

Underscore.js, I have this kind of object foo(2334). and I want it to be findWhere(not like foo(not like 34)), when the property value is 233500000000-0000 (yearly or quarterly). Underscore.js : findWhere with nested property value. Underscore.js findWhere nested objects. 0. Sorting an array of objects by property values.

You can do this with _.where alone.

var filteredList  = _.where(list, { tax:[{id:17}] });

Edit: This method works in older version of lodash, but wont work in current underscore or lodash. Working example anyway: https://jsfiddle.net/rhp4crma/

Underscore: findWhere with nested property value, Underscore.js findWhere nested objects How can I do a deep, nested search. findDeep: function(items, attrs) {; function match(value) {; for (var key in attrs)  Underscore.js | findWhere() with Examples The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects.

Underscore.js findWhere nested objects, Should _.where iterate over every property to search for that This thread prompted me to write a long needed lens library for JavaScript: array of nested Objects. and returns the required key/s and value/s. as a new object. underscore.js mixin for plucking nested properties - _.deep.js. Worth noting, you can use native Underscore (>= v1.8.0) to achieve this by using _.property along with _.compose.

_.where() to find multi level keys · Issue #1207 · jashkenas , If I have an array of arrays (or objects). How do I find a child element which matches a particular key value? For example [ [ name Something exactly along the lines of Underscore js' where and findWhere implementations  result_.result(object, property, [defaultValue]) If the value of the named property is a function then invoke it with the object as context; otherwise, return it. If a default value is provided and the property doesn't exist or is undefined then the default will be returned.

Find element in nested array by child key-value? · Issue #24 , And here is UnderscoreJS code of _.find(), _.where(), _. use case of `find`: getting the first object // containing specific `key:value` pairs. _. I have created a package/ function which can search deep down in nested Object or in array of nested Objects. and returns the required key/s and value/s. as a new object. Please have a look to it. Even if you have multiple occurances of a same name key. It will return an array of all values as the value of that key name.

Comments
  • This could be a candidate for CR ...
  • Fist time using underscore, First Roadbump, First Link on Google, FTW!
  • Also if tax is just an object, you must put it on an array to this thing work. Something like this _.some([obj.tax], {id: ID_TO_FIND});
  • I think that this answer is more performant than the accepted answer above.
  • can i use _.findWhere inside filter?
  • Can someone tell me why this is being downvoted? I tested it and it returns the exact result OP asked for with only 1 command.
  • I tried this and it does not work. Mine returns undefined although the item is present
  • correct syntax is : var filteredList = _.where(list, { tax:{id:17} });
  • I have up-voted your answer to encourage you to participate in SO, please update your answer with a link to jsfiddle example for this specified case and update your answer corrector as @Sam suggested.