Why didn't lodash "some" function work as expected?

I am trying to use lodash 2.4.1 to find out if at least one element exists in an array with trueas its value.

So, I decided to use the lodash someor function any.

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

What never happens inside an if block, even if the first condition really evaluates to true.

I got:

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

Before the block ifand I can see the first statement evaluating the value true.

What could it be unsuccessful?

I am using lodash 2.4.1 as it includes a Sails js dependency.

+4
source share
5 answers

change

, _.some [docs] :

_.some(bool_arr) 

.

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>
 
Hide result

, Boolean . :

_.some(bool_arr, _.identity)

_.identity [docs]

.

+3

:

function isTrue(v) {
  return v === true;
}

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

Lodash doc: https://lodash.com/docs#some

+2

, Boolean ( )

+2

- some, lodash , . true, item1[true], item2[true] .. , .

The solution is to completely omit the second argument, in which case it is by default equal to identity, that is, the element itself.

+2
source

_.somechecks that the collection you have collected contains at least one instance of the object of the type that you give it. Note:

> _.some([true, false], true)
false

> _.some([true, undefined], Boolean)
true

Instead, you can pass a function like this:

> _.some([true, false], function(value) { return value === true; })
true

> _.some([undefined, false], function(value) { return value === true; })
false

However, I would suggest using if (lineup.reachesBudgetLimit() || lineup.reachesMaxForeignPlayers()).

+1
source

All Articles