admin管理员组文章数量:1287667
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.
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.
Share Improve this question asked Jun 12, 2015 at 20:45 diegoaguilardiegoaguilar 8,37616 gold badges85 silver badges133 bronze badges5 Answers
Reset to default 3edit:
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./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.
I believe you meant to pass Boolean
as the predicate (2nd argument)
When you pass a non-function value to some
, lodash treats it as a property name and tries to find an object which contain a non-falsy property with the name given. In your example, you pass true
, so it tries to access item1[true]
, item2[true]
etc. Since none of them exists, the result is false.
The solution is to omit the second argument altogether, in which case it defaults to identity
, that is, to the element itself.
You should use a function:
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./docs#some
_.some
is checking that the collection you give it contains at least one instance of an object of the type you are giving it. Observe:
> _.some([true, false], true)
false
> _.some([true, undefined], Boolean)
true
You can instead pass a function like so:
> _.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())
.
本文标签: javascriptWhy wouldn39t lodash 39some39 function work as expectedStack Overflow
版权声明:本文标题:javascript - Why wouldn't lodash 'some' function work as expected? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741315500a2371889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论