Javascript 使用变量作为JSON键
我想缩短我的代码,我被卡住了。我有一个小测验,每个问题的答案都会被载入。但我想有一个变量来为我做到这一点 代码如下:Javascript 使用变量作为JSON键,javascript,json,Javascript,Json,我想缩短我的代码,我被卡住了。我有一个小测验,每个问题的答案都会被载入。但我想有一个变量来为我做到这一点 代码如下: if (nr === 1) { $('#questionmobile').html(content.inst3.question1); $('#answer1').html(content.inst3.q1a1); $('#answer2').html(content.inst3.q1a2); $('#answer3').html(content.
if (nr === 1) {
$('#questionmobile').html(content.inst3.question1);
$('#answer1').html(content.inst3.q1a1);
$('#answer2').html(content.inst3.q1a2);
$('#answer3').html(content.inst3.q1a3);
$('#answer4').html(content.inst3.q1a4);
} else if (nr === 2) {
$('#questionmobile').html(content.inst3.question2);
$('#answer1').html(content.inst3.q2a1);
$('#answer2').html(content.inst3.q2a2);
$('#answer3').html(content.inst3.q2a3);
$('#answer4').html(content.inst3.q2a4);
}......
正如您所看到的,它是非常冗余的,所以我想包括变量“nr”,它包含问题数量的信息。所以我想要这样的东西:
$('#questionmobile').html(content.inst3.question+nr);
$('#answer1').html(content.inst3.q+nr+a1);
{
"questions": [
{
"prompt": "Who was the first president of the US?",
"answers": ["George Washington", "Abraham Lincoln", "Captain America", "The Hulk"]
}
]
}
连接
+nr+
不起作用,因为它不能指向正确的JSON内容。如果要将变量用作键,可以使用括号表示法(如数组,[]
)
$('#questionmobile').html(content.inst3['question'+nr]);
$('#answer1').html(content.inst3['q'+nr+'a1']);
您甚至可以使用循环来获得答案:
$('#questionmobile').html(content.inst3['question'+nr]);
var i, numAnswers = 4;
for(i = 1; i <= numAnswers; i++) {
$('#answer' + i).html(content.inst3['q'+nr+'a'+i]);
}
$('questionmobile').html(content.inst3['question'+nr]);
var i,numAnswers=4;
对于(i=1;i,如果要将变量用作键,可以使用括号表示法(如数组,[]
)
$('#questionmobile').html(content.inst3['question'+nr]);
$('#answer1').html(content.inst3['q'+nr+'a1']);
您甚至可以使用循环来获得答案:
$('#questionmobile').html(content.inst3['question'+nr]);
var i, numAnswers = 4;
for(i = 1; i <= numAnswers; i++) {
$('#answer' + i).html(content.inst3['q'+nr+'a'+i]);
}
$('questionmobile').html(content.inst3['question'+nr]);
var i,numAnswers=4;
对于(i=1;i首先,当你说content.inst3.q+nr+a1
时,JavaScript将其解释为(content.inst3.q)+(nr)+(a1)
,因此它将不存在的变量添加到一起
您所问问题的答案是,您可以使用括号通过字符串名称访问对象中的字段。例如:
var x = {name: "Joe", age:56};
alert(x["name"]); //Outputs Joe
alert(x["age"]); //Outputs 56
您可以使用相同的技术来编写与模式匹配的字符串。但是,这可能不是最佳方法。您可能应该重新构造输入数据,以便不需要使用此技术。例如,您可以使数据看起来像:
$('#questionmobile').html(content.inst3.question+nr);
$('#answer1').html(content.inst3.q+nr+a1);
{
"questions": [
{
"prompt": "Who was the first president of the US?",
"answers": ["George Washington", "Abraham Lincoln", "Captain America", "The Hulk"]
}
]
}
这将把你的数据组织成数组,这似乎更符合你对这些数据的用例。首先,当你说content.inst3.q+nr+a1
时,JavaScript将其解释为(content.inst3.q)+(nr)+(a1)
,因此它将不存在的变量添加在一起
您所问问题的答案是,您可以使用括号通过字符串名称访问对象中的字段。例如:
var x = {name: "Joe", age:56};
alert(x["name"]); //Outputs Joe
alert(x["age"]); //Outputs 56
您可以使用相同的技术来编写与模式匹配的字符串。但是,这可能不是最佳方法。您可能应该重新构造输入数据,以便不需要使用此技术。例如,您可以使数据看起来像:
$('#questionmobile').html(content.inst3.question+nr);
$('#answer1').html(content.inst3.q+nr+a1);
{
"questions": [
{
"prompt": "Who was the first president of the US?",
"answers": ["George Washington", "Abraham Lincoln", "Captain America", "The Hulk"]
}
]
}
这将把您的数据组织成数组,这似乎更符合您的数据用例。谢谢大家,两个答案都有帮助。我的解决方案是将您的答案混合在一起
$('#questionmobile').html(content.inst3.questions[nr - 1].question);
var i, numAnswers = 3;
for (i = 0; i <= numAnswers; i++) {
$('#answer' + (i + 1)).html(content.inst3.questions[nr - 1].answers[i]);
}
谢谢大家,两个答案都有帮助。我的解决方案是把你们的答案混合在一起
$('#questionmobile').html(content.inst3.questions[nr - 1].question);
var i, numAnswers = 3;
for (i = 0; i <= numAnswers; i++) {
$('#answer' + (i + 1)).html(content.inst3.questions[nr - 1].answers[i]);
}