我正在尝试使用 lodash 2.4.1 来了解数组中是否至少有一个元素的值为 true
。
所以我决定使用 lodash some
或 any
函数。
我的代码是这样的:
if ( _.some([lineup.reachesMaxForeignPlayers(), lineup.reachesBudgetLimit()], true) ) {
response.send(400, "La inclusión de este jugador no satisface las reglas del juego");
}
从不进入 if block ,即使第一个条件实际上计算为真。
我得到了:
console.log(lineup.reachesMaxForeignPlayers());
console.log(lineup.reachesBudgetLimit());
在 if
block 之前,我实际上可以看到第一个语句评估为 true
。
它可能失败了什么?
我使用 lodash 2.4.1,因为它包含 Sails js 依赖项。
最佳答案
编辑:
实际上,只是使用_.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>
除了建议将 Boolean
作为谓词传递的其他答案。您还可以使用:
_.some(bool_arr, _.identity)
_.identity
[docs]This method returns the first argument provided to it.
关于javascript - 为什么 lodash 'some' 函数不能按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30811904/