对于循环未正确访问 JSON 对象
For loop not accessing JSON Objects properly?
由于某种原因,当我完成测验时,无法访问正确的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);
}
}
更简洁的版本是将类型放在哈希图中,并通过给定的类型Concat访问类型:
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]);
相关文章:
- 访问JSON对象内部的数组元素
- 访问json数组中的对象
- 如何访问JSON元素
- 使用Backbone.js访问JSON响应的部分
- JS/JON-通过JS访问JSON中的多个数组
- 对于循环未正确访问 JSON 对象
- 使用 jasmine-node / frisby 访问 JSON 值
- 如何访问 JSON 对象数组的第一个元素
- 如何在 for 循环中访问 json 数组字段的名称
- Javascript 不会访问 json 对象
- 无法访问JSON对象的值
- 无法访问json对象中的数据
- 访问JSON对象中的特定值
- 如何通过javascript访问JSON数据
- 从Web服务访问JSON响应
- 我可以't访问JSON中的第一个对象
- 是否有可能访问JSON对象中列出的jQuery选择器
- 为什么我可以't使用点表示法访问JSON字典元素
- 访问JSON数组时出现问题
- 如何在processing.js中访问json数据