I want to know whether the given value exists in javascript array or not.
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 tried the following,
var exists = _.where(array, {name: 'xxx'});
It return the obj {'id': 1, 'name': 'xxx'}. It works as expect.
Here I need to check exists.length > 0 to find whether it exists or not
But is there any other function of get the same.
Since if the function return true if exists and false if not, It would be better.
_.where(array, {name: 'xxx'}).length > 0_.wherehas to find them all,_.findand_.findWherewill short-circuit and return as soon as they've found a match so they're more appropriate for existence checks.