I\'m new to Underscore js and bit confused on how to use it. I have a collection of \'goals\' and I want to find one of it by ID.
here\'s the data:
{
You are using Array of objects. So,you can use: _.findWhere(Looks through the list and returns the first value that matches all of the key-value pairs ) to get the all the properties based on id or other key attribute.
var some= [
{Employee:'ved',id:20},
{Employee:"ved",age:25},
{Employee:"p",age:2}
];
var a = _.findWhere(some,{id:20});
console.log('searchResult',a);
To get the index, you can use something like this:
var b = _.indexOf(some,a);
console.log('index',b);
If you need all list of uses,
TRY: _.where(It looks through each occurrence in the array, returning an array of all the values that contain the key-value pairs listed in properties.)
var some= [
{Employee:"ved",id:20},
{Employee:"ved prakash",id:20},
{Employee:"anyone",id:2}
];
var a = _.where(some,{id:25});
console.log('searchResult',a);
_.find: It is used to check the value only, not Key-value both.
Visit Docs:_.find