Xavier C. Xavier C. - 2 years ago 50
Javascript Question

Test if value is in array with underscoreJS using _.some method

let array = [1234, 1233, 1232];

console.log(_.some(array, 1234));

It returns
. Do you know why?

Answer Source

As per the documentation of _.some() method, second argument should be a predicate function

console.log(_.some(array, function(v){ return v === 1234}));

In this particular case you can simply use native javascript Array#indexOf method.

console.log(array.indexOf(1234) > -1);

Also there is native JavaScript Array#some method.

console.log(array.some(function(v){ return v === 1234}));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download