gpt4 book ai didi

javascript - 返回带有过滤子数组的父对象

转载 作者:行者123 更新时间:2023-11-29 10:40:50 24 4
gpt4 key购买 nike

示例数据:

{
"QuestionId": 2,
"QuestionName": "Question2",
"QuestionTypeId": 2,
"QuestionTypeName": "CheckBoxList",
"Choices": [{
"ChoiceId": 7,
"ChoiceName": "Choice1",
"Answer": "7"
},
{
"ChoiceId": 8,
"ChoiceName": "Choice2",
"Answer": ""
},
{
"ChoiceId": 9,
"ChoiceName": "Choice3",
"Answer": ""
},
{
"ChoiceId": 10,
"ChoiceName": "Choice4",
"Answer": "10"
}
]
}

根据示例数据,如何使用 Underscore 或任何更好的 javascript 方法通过过滤后的 Choices 保持相同的结构返回? Choice 是根据 Choice.Answer 过滤的。答案必须有一个值。

最佳答案

如果您想过滤掉答案为空的选项,请使用 Array.filter - 我还假设您的上述数据结构在一个数组(一组问题)中

//Loop over each question
for (var i = 0; i < questions.length; i++) {
//For each question, filter the "Choices" array to not include empty Answers
questions[i].Choices = questions[i].Choices.filter(function(choice) {
return choice.Answer != ""
})
}

关于javascript - 返回带有过滤子数组的父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659729/

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