Skip to content Skip to sidebar Skip to footer

How To Find Child Object With Property Value Using Underscorejs And Jquery?

Take the following data: object { name: 'foo' children: [ { 'name': 'cat' 'color': 'green' }, { 'name': 'dog' 'color': '

Solution 1:

In JQuery you can use filter to get the child object.

var obj = {
   name: 'foo',
   children: [
      {
         'name': 'cat',
         'color': 'green'
      },
      {
         'name': 'dog',
         'color': 'blue'
      },
      {
         'name': 'bird',
         'color': 'red'
      }
   ]
};

console.log(obj.children.filter(function(e){return e.name == "dog";})[0]);
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Solution 2:

With Underscore.js you could simply use a filter function such as:

var child = _.filter(object.children, obj => obj.name === 'dog');

You don't need jQuery for that.

Post a Comment for "How To Find Child Object With Property Value Using Underscorejs And Jquery?"