I want to get the index of the given value inside a Array using underscore.js.
Here is my case
var array = [{'id': 1, 'name': 'xxx'},
{'id': 2, 'name': 'yyy'},
{'id': 3, 'name': 'zzz'}];
var searchValue = {'id': 1, 'name': 'xxx'};
I used the following code,
var index = _.indexOf(array, function(data) {
alert(data.toSource()); //For testing purpose
return data === searchValue;
});
Also tried this too
var index = _.indexOf(array, {id: searchValue.id});
But it returns -1
. Since it does not enter into that function. So I didn't get that alert message.
Whats wrong with my code. Can anyone help me?