gpt4 book ai didi

javascript - 为什么 lodash 'some' 函数不能按预期工作?

转载 作者:搜寻专家 更新时间:2023-11-01 05:00:24 26 4
gpt4 key购买 nike

我正在尝试使用 lodash 2.4.1 来了解数组中是否至少有一个元素的值为 true

所以我决定使用 lodash someany 函数。

我的代码是这样的:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com