Xavier C. Xavier C. - 5 months ago 8
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
false
. Do you know why?

Answer

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