gpt4 book ai didi

javascript - 如何使用 if 语句检查数组中的任何值是否显示 true 值?

转载 作者:行者123 更新时间:2023-11-30 17:33:58 24 4
gpt4 key购买 nike

我有以下对象:

var myObj = 
{"qId":"726112",
"text":"xx",
"answers":[{"answerId":null,
"answerUId":1,
"text":"xx",
"correct":null,
"response":false},
{"answerId":null,
"answerUId":2,
"text":"xx",
"correct":null,
"response":false},
{"answerId":null,
"answerUId":4,
"text":"xx",
"correct":null,
"response":false}]}

谁能告诉我如何使用 if 语句来检查是否有任何一个响应字段的值为 true?

最佳答案

您可以使用 Array.prototype.some函数,如果至少有一个元素返回 true,则返回 true,如下所示

if (myObj.answers.some(function(answer) { return answer.response; })) {
# Atleast one of them is true
}

Working demo

关于javascript - 如何使用 if 语句检查数组中的任何值是否显示 true 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423577/

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