2017-04-27 36 views
2
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 undefinedjQuery的多維數組的定義

我如何定義多維數組? 我試過像var questions = [];但它不工作太...

+0

爲什麼標記爲「jQuery」?可以刪除'jQuery'標籤嗎? – evolutionxbox

回答

2

我認爲你在尋找什麼是對象的數組:

// 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] 
    }); 
} 
0

你需要初始化對象。只需編寫

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]; 
} 
0

你差不多在那裏有幾個更正。

錯誤1:

var questions = {}; 

即是一個對象的聲明。不是數組。

錯誤2:

questions[i]["questions"] 

要做到這一點,你必須在questionsi位置有一個對象。

所以改正後,

var questions = []; //declare arry 
for (var i = 0; i < tmp_questions.length; i++) { 
    questions.push({}); //push it 
    questions[i]["questions"] = tmp_questions[i]; // add properties 
    questions[i]["input_type_id"] = tmp_question_types[i]; 
    questions[i]["choices"] = tmp_choices[i]; 

} 
+0

@ibrahimmahrir啊..我對這個錯誤很不好。糾正。 –

0

,在插入之前初始化。 檢查這個

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);

0

你必須創建你的對象到你的數組中前推數據到它:

var questions = []; // here is an array 
    for (var i = 0; i < tmp_questions.length; i++) { 
     questions[i] = {}; // here is an empty object 
     questions[i]["questions"] = tmp_questions[i]; 
     questions[i]["input_type_id"] = tmp_question_types[i]; 
     questions[i]["choices"] = tmp_choices[i]; 
    } 

其實你可以這樣做:

var questions = []; 
for(...){ 
    questions[i] = { 
     questions : tmp_questions[i], 
     "input_type_id" : tmp_question_types[i], 
     choices : tmp_choices[i] 
    } 
} 

希望有所幫助, 保羅Ĵ

0

你的陣列是不是Multi-dimensional Array,只是對象的數組。 多維數組應該是這樣的:

var bidimensionalArray = new Array(new Array()); 
var tridimensionalName = new Array(new Array(new Array())); 

總之,爲了創造一種可以包含對象一個數組:「問題,IDS和選擇」,你必須在陣列中的每個元素進行初始化。

見下面請:

var tmp_questions = ["test1", "test2"]; 
 
var tmp_question_types = ["type1", "type2"]; 
 
var tmp_choices = ["choice1", "choice2"]; 
 

 

 
var questions = new Array(); 
 
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);

我希望它可以幫助你,再見。