【发布时间】:2015-06-22 07:25:38
【问题描述】:
我有以下二维数组代码
var questions = [
['How many states are in the United States?', 50],
['How many continents are there?', 7],
['How many legs does an insect have?', 6]
];
并将其转换为数组对象
var questions = [
{ question: 'How many states are in the United States?', answer: 50 },
{ question: 'How many continents are there?', answer: 7 },
{ question: 'How many legs does an insect have?', answer: 6 }
];
并有相应的for循环。
for (var i = 0; i < questions.length; i += 1) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
response = parseInt(response);
if (response === answer) {
correctAnswers += 1;
correct.push(question);
} else {
wrong.push(question);
}
}
和
for (var i = 0; i < questions.length; i += 1) {
question = questions[i].question;
answer = questions[i].answer;
response = prompt(question);
response = parseInt(response);
if (response === answer) {
correctAnswers += 1;
}
}
二维数组和数组对象的实际区别是什么?它会影响运行for循环更快地对数据进行排序吗?我怎么知道哪个更好?
【问题讨论】:
-
这是一种使用数组的糟糕方式。
questions[i][1]那是什么?questions[i].answer其实很有道理。 -
它以答案元素为目标。任务是回答测验问题......学习JavaScript并被提及
标签: javascript arrays for-loop multidimensional-array