1
let array = [1234, 1233, 1232];

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

It returns false. Do you know why?

5
  • 1
    console.log(_.some(array, function(v){ return v === 1234})); Commented Jun 14, 2016 at 10:19
  • Ok fine thanks! Do you maybe know a simpler method to test if a value is in array with underscore? Commented Jun 14, 2016 at 10:22
  • in this case you can simple use indexOf() Commented Jun 14, 2016 at 10:23
  • You could also use _.contains e.g _.contains(array, 1234) Commented Jun 14, 2016 at 10:49
  • Yes, I know why, because I read the documentation for _.some. Commented Jun 24, 2016 at 5:12

2 Answers 2

2

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

with ES6 arrow function

console.log(array.some(v => v == 1234))
Sign up to request clarification or add additional context in comments.

1 Comment

There is also ES5 some and ECMAScript 2015 arrow functions to give: array.some(v=>v==1234). ;-)
0

With UNDERSCORE.JS you can simply use,

console.log(_.indexOf(array, 1234) >= 0)

Document for more details: http://underscorejs.org/#indexOf

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.