2017-04-27 3 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 mehrdimensionales Array Definition

Wie kann ich das mehrdimensionale Array definieren? Ich versuchte wie var questions = []; aber es funktioniert nicht auch ...

+0

Warum ist dies markiert "jQuery"?Darf das 'jQuery'-Tag entfernt werden? – evolutionxbox

Antwort

2

denke ich, was Sie suchen sie ein Array von Objekten ist:

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

oder in einer klaren Art und Weise wie folgt aus:

// 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

Sie müssen das Objekt zuerst initialisieren. Schreiben Sie einfach

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

Sie sind fast da mit mehreren Korrekturen.

Fehler 1:

var questions = {}; 

Das ist eine Objektdeklaration ist. Nicht Array. 2

Fehler:

questions[i]["questions"] 

zu tun, dass Sie ein Objekt an Position i von questions haben muss.

So nach Korrekturen,

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 Ahh .. mein schlechtes für den Tippfehler. Korrigiert. –

0

Sie müssen vor dem Einsetzen initialisieren. prüfen diese

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

Sie müssen Ihr Objekt in Ihr Array erstellen Daten in diese Vordergrund zu drängen:

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

eigentlich könnten Sie tun:

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

Hoffnung, die helfen Paul J

0

Ihr Array ist kein Multi-dimensional Array, sondern nur ein Array von Objekten. Ein mehrdimensionales Array sollte man wie:

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

Wie auch immer, um ein Array zu erstellen, könnte ein Objekt enthält mit: „Fragen, ids und die Wahlen“, müssen Sie jedes Element des Arrays initialisieren.

Siehe folgende bitte:

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

Ich hoffe, es hilft Ihnen, bye.