gpt4 book ai didi

javascript - For 循环无法正确访问 JSON 对象?

转载 作者:行者123 更新时间:2023-12-03 07:03:35 25 4
gpt4 key购买 nike

由于某种原因,当我完成测验时,没有访问正确的 JSON 对象属性。一旦字符串被识别,它就应该访问该属性。如果我硬编码它。 [0]、[1] 等有效。我怎样才能解决这个问题?另外,如何使这段代码更简洁?我觉得除了使用这么多 if 语句之外,可能还有另一种方法。谢谢。

有问题的对象

var personTypes = [{ 
type : "INTJ",
typeInfo: "Imaginative and strategic thinkers, with a plan for everything.",

},
{type : "INTP",
typeInfo: "Innovative inventors with an unquenchable thirst for knowledge.",

},
{type : "ENTJ",
typeInfo: "Bold, imaginative and strong-willed leaders, always finding a way – or making one.",

},
{type : "ENTP",
typeInfo: "Smart and curious thinkers who cannot resist an intellectual challenge.",

},
{type : "INFJ",
typeInfo: "Quiet and mystical, yet very inspiring and tireless idealists",

},
{type : "INFP",
typeInfo: "Poetic, kind and altruistic people, always eager to help a good cause.",

},
{type : "ENFJ",
typeInfo: "Charismatic and inspiring leaders, able to mesmerize their listeners.",

},
{type : "ENFP",
typeInfo: "Enthusiastic, creative and sociable free spirits, who can always find a reason to smile.",

},
{type : "ISTJ",
typeInfo: "Practical and fact-minded individuals, whose reliability cannot be doubted.",

},
{type : "ISFJ",
typeInfo: "Very dedicated and warm protectors, always ready to defend their loved ones.",

},
{type : "ESTJ",
typeInfo: "Excellent administrators, unsurpassed at managing things – or people.",

},
{type : "ESFJ",
typeInfo: "Extraordinarily caring, social and popular people, always eager to help.",

},
{type : "ISTP",
typeInfo: "Bold and practical experimenters, masters of all kinds of tools.",

},
{type : "ISFP",
typeInfo: "Flexible and charming artists, always ready to explore and experience something new.",

},
{type : "ESTP",
typeInfo: "Smart, energetic and very perceptive people, who truly enjoy living on the edge",

},
{type : "ESFP",
typeInfo: "Spontaneous, energetic and enthusiastic people – life is never boring around them.",

},
];

访问对象

 // once done with quiz
if (questionNum === 3) {

//concat radio inputs
var typeConcat = this.ei + this.sn+ this.tf+ this.pj;
//output concat to screen
$("p").show();
$("h2").text("Your type is " + typeConcat);



//use this data inside the <p>
for (i = 0; i < personTypes.length; i++) {
if (typeConcat=="INTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="INTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="INFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="INFP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ENFP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESTJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESFJ") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ISFP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESTP") {
$("p").text(personTypes[i].typeInfo);
}
if (typeConcat=="ESFP") {
$("p").text(personTypes[i].typeInfo);
}
}
}

最佳答案

我认为当您指定了类型时,您不需要所有的 if 语句,对吧?这会起到同样的作用。

编辑:不要忘记在 i = 0 之前包含 var

for (var i = 0; i < personTypes.length; i++) {    
if (typeConcat==personTypes[i].type) {
$("p").text(personTypes[i].typeInfo);
}
}

关于javascript - For 循环无法正确访问 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36924152/

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