Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why wouldn't lodash 'some' function work as expected?

I'm trying to use lodash 2.4.1 in order to know if there's at least one element within an array with true as its value.

So I decided to use lodash some or any function.

This is what my code looks like:

if ( _.some([lineup.reachesMaxForeignPlayers(), lineup.reachesBudgetLimit()], true) ) {
  response.send(400, "La inclusión de este jugador no satisface las reglas del juego");
}

Which is never going inside the if block, even that first condition actually evaluates to true.

I got:

console.log(lineup.reachesMaxForeignPlayers());
console.log(lineup.reachesBudgetLimit());

Before the if block and I can actually see first statement evaluating to true.

What could it be failing?

I use lodash 2.4.1 as it's included Sails js dependency.

like image 631
diegoaguilar Avatar asked Dec 25 '22 18:12

diegoaguilar


1 Answers

edit:

Actually, just using _.some[docs] with no predicate defaults to identity:

_.some(bool_arr) 

should work.

console.log(_.some([true, true, false]));
console.log(_.some([false, false, false]));
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.9.3/lodash.min.js"></script>
 

In addition to the other answers that suggest passing Boolean as the predicate. You can also use:

_.some(bool_arr, _.identity)

_.identity[docs]

This method returns the first argument provided to it.

like image 87
dting Avatar answered Jan 05 '23 18:01

dting