jQuery 多维数组定义
2017-04-27
96
var questions = {};
for (var i = 0; i < tmp_questions.length; i++) {
questions[i]["questions"] = tmp_questions[i];
questions[i]["input_type_id"] = tmp_question_types[i];
questions[i]["choices"] = tmp_choices[i];
}
Uncaught TypeError: Cannot set property 'questions' of undefined
我该如何定义该多维数组?
我尝试过
var questions = [];
但不起作用...
3个回答
我认为您正在寻找的是一个对象数组:
// this should be an array
var questions = [];
for (var i = 0; i < tmp_questions.length; i++) {
// for each iteration, create an object and fill it
questions[i] = {};
questions[i]["questions"] = tmp_questions[i];
questions[i]["input_type_id"] = tmp_question_types[i];
questions[i]["choices"] = tmp_choices[i];
}
或者像这样清晰地表示:
// this should be an array
var questions = [];
for (var i = 0; i < tmp_questions.length; i++) {
// for each iteration, push an object like so
questions.push({
"questions": tmp_questions[i],
"input_type_id": tmp_question_types[i],
"choices": tmp_choices[i]
});
}
ibrahim mahrir
2017-04-27
您需要先初始化对象。只需写入
questions = [];
for (var i = 0; i < tmp_questions.length; i++) {
questions[i] = {};
questions[i]["questions"] = tmp_questions[i];
questions[i]["input_type_id"] = tmp_question_types[i];
questions[i]["choices"] = tmp_choices[i];
}
Torben
2017-04-27
插入前必须初始化。 检查一下
var tmp_choices= tmp_question_types = tmp_questions = [1,2,3,4,5];
var questions = []; // If you want questions to be array of objects then use []
for (var i = 0; i < tmp_questions.length; i++) {
questions[i] = {};
questions[i]["questions"] = tmp_questions[i];
questions[i]["input_type_id"] = tmp_question_types[i];
questions[i]["choices"] = tmp_choices[i];
}
console.log(questions);
Aman Rawat
2017-04-27