由于某些原因,当我完成测验时,不会访问正确的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);
}
}
}
答案 0 :(得分:1)
当你指定了类型时,我认为你不需要所有的if语句,对吗?这可能会起作用。
编辑:不要忘记在var
之前加入i = 0
。
for (var i = 0; i < personTypes.length; i++) {
if (typeConcat==personTypes[i].type) {
$("p").text(personTypes[i].typeInfo);
}
}
答案 1 :(得分:0)
更简洁的版本是将类型放入散列图并通过typeConcat访问类型:
var personTypes = {
INTJ: "Imaginative and strategic thinkers, with a plan for everything.",
INTP: "Innovative inventors with an unquenchable thirst for knowledge.",
ENTJ: "Bold, imaginative and strong-willed leaders, always finding a way – or making one.",
ENTP: "Smart and curious thinkers who cannot resist an intellectual challenge.",
INFJ: "Quiet and mystical, yet very inspiring and tireless idealists",
INFP: "Poetic, kind and altruistic people, always eager to help a good cause.",
ENFJ: "Charismatic and inspiring leaders, able to mesmerize their listeners.",
ENFP: "Enthusiastic, creative and sociable free spirits, who can always find a reason to smile.",
ISTJ: "Practical and fact-minded individuals, whose reliability cannot be doubted.",
ISFJ: "Very dedicated and warm protectors, always ready to defend their loved ones.",
ESTJ: "Excellent administrators, unsurpassed at managing things – or people.",
ESFJ: "Extraordinarily caring, social and popular people, always eager to help.",
ISTP: "Bold and practical experimenters, masters of all kinds of tools.",
ISFP: "Flexible and charming artists, always ready to explore and experience something new.",
ESTP: "Smart, energetic and very perceptive people, who truly enjoy living on the edge",
ESFP: "Spontaneous, energetic and enthusiastic people – life is never boring around them."
}
$("p").text(personTypes[typeConcat]);