I need assistance with finding the index of a specific value within an array using underscore.js.
Here is the scenario I am facing:
var array = [{'id': 1, 'name': 'xxx'},
{'id': 2, 'name': 'yyy'},
{'id': 3, 'name': 'zzz'}];
var searchValue = {'id': 1, 'name': 'xxx'};
I have attempted to use the following code:
var index = _.indexOf(array, function(data) {
alert(data.toSource()); //For testing purpose
return data === searchValue;
});
I also tried this alternative approach:
var index = _.indexOf(array, {id: searchValue.id});
Unfortunately, both methods return -1
which indicates that the function was not entered. As a result, the alert message did not appear.
I am seeking help in identifying what is incorrect with my code. Can anyone provide guidance?