简体   繁体   中英

Inserting values into multi-dimentional javascript array

I need to dynamically create a multidimensional javascript array that matches this layout:

array_answers[0][1]:"yes"
array_answers[1][2]:"no"
array_answers[2][2-subquestion]:"text input"
array_answers[3][8]:"yes"
array_answers[4][8-subquestion]:"text input"

The first "[ ]" defines what question it is on the page (out of totalInputs )
The second "[ ]" defines what question from the database this is ( questions already in order to match the corresponding input)
and the information following is the input I am trying to add

I have attempted to the following with no luck.

for(var i = 0; i < totalInputs; i++) {
  array_answers.push([i]);
  array_answers[i].push([questions[i]]);
  array_answers[i][0] = "yes, no, or other text";
}

The last line is where it falls apart. It would make sense to me that I should be able to use [0] to indicate that I want the first array to be given this value but with no avail.
I have also tried:

for(var i = 0; i < totalInputs; i++) {
  array_answers.push([i]);
  array_answers[i].push([questions[i]]);
  array_answers[i][questions[i]] = "yes, no, or other text";
}

but this gives me lots of empty arrays for all the numbers from 0 to whatever the value of questions[i] is.

What am I missing or is there a simpler way to do this in jQuery while still conforming to the target layout?

If I understand you correctly, you are trying to store questions and prompts (maybe?) together in a multi dimensional array. let me suggest a different way that should work.

const array_answers = questions.map(q => [q, "yes, no, other text"]);

This may be what you want

I am still not 100% sure of what you need, but i thought I would write an answer with my assumptions, that I can update as the information improves.

The first "[ ]" defines what question it is on the page (out of totalInputs)

This part looks like you are correct, and need to use an array. But an array is just a list of "things", in your case, questions. So where you have this line:

array_answers.push([i]);

I'm not sure it is doing what you are expecting. This is adding a new array item, which is in itself an array, that contains a single number. So if totalInputs is 3, then that first line will result in this structure:

array_answers= [[0],[1],[2]]

I think what you actually might intend here is to simply house an array of question details. Now by the complexity of your keys listed for the second dimension, it looks like an object would be more appropriate.

The second "[ ]" defines what question from the database this is (questions already in order to match the corresponding input)

So lets go ahead and create a single question.

var question = {
  anythingYouWant: 'here',
  2: 'even numeric keys'
}
// you can access the values in these ways
console.log(question.anythingYouWant)
console.log(question[2])
console.log(question['anythingYouWant'])

Now once you have a question object, you can then add it to your array_answers array with push.

array_answers.push(question).

If you have two identical questions like the one above, your array will look like this:

array_answers = [{
  anythingYouWant: 'here',
  2: 'even numeric keys'
},{
  anythingYouWant: 'here',
  2: 'even numeric keys'
}]

In order to access the questions within the array, you can simply use their index:

// access second question
var secondQuestion = array_answers[1]

You can read these links to learn more about objects & arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM