Xavier C. Xavier C. - 4 months ago 5
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?


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}));