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 undefined

How can i define that multidimensional array ? I tried like var questions = []; but it does not work too...

1
  • Why is this tagged jQuery? May the jQuery tag be removed? Commented Apr 27, 2017 at 10:26

6 Answers 6

2

I think what you're looking for is an array of objects:

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

or in a clear way like this:

// 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]
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

You Need to initialize the object first. Just write

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

Comments

0

You have to initialize before inserting. Check this

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

Comments

0

You have to create your object into your array before pushing data into it :

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

actually you could do :

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

Hope that help, Paul J

Comments

0

You are almost there with several corrections.

Error 1 :

var questions = {}; 

That is an object declaration. Not array.

Error 2 :

questions[i]["questions"]

To do that you must have an object at position i of questions.

So after corrections,

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

}

1 Comment

@ibrahimmahrir Ahh.. my bad for the typo. Corrected.
0

Your array is not a Multi-dimensional Array, but just an array of objects. A multidimensional array should be one like:

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

Anyway, in order to create an array that could contains an object with: "questions, ids and choices", you have to initialize every element of the array.

See following please:

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

I hope it helps you, bye.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.