Multi-dimensional Array: objects inside an Array

Hello, Everyone
I have been working javascript array. My biggest challenge is with multi-dimensional arrays, especially ones containing an object. My biggest challenge is how loop through arrays containing objects, second how to index those array. Third, how can we add html inputs into those array. here is an an example:

var questions = [
{
question: "what is the name of first man on the moon \n (a) Neil Amstrong \n(b) Charles Lindeberg \n (c) Albert Einstein \n (d) All the above \n",

answer: "a"
},

{
question: "who invented the telescope \n (a) Thomas Edison \n (b) Galileo Galilei \n (c) Alexandra Graham bell \n (d) All the above \n",

answer: "b"
}
]

I get undefined when i do this:

for(var i = 0; questions.length; i++){
	var user = questions[i];
	console.log(user);
}

I get error messages when i do this:

for(var i = 0; questions.length; i++){
	var user = questions[i].question;
	console.log(user);
}
`````
What code do you suggest?

Hello, please are you willing to help or not.

people have lifes outside the forum, when they can, want, like, they drop here to help other people
it is still a forum tho

you need a stopping condition, you have an infinite loop like this
fix that, then try again what you were trying to do

1 Like

Then just say you dont wanna help, because you have live. Btw, I also have live. I am trying to improve the quality of my life by learning new skills. If you dont wanna help just say so.

Hello, Everyone
I have been working on how to reference, loop through and access a multi-dimension array which contains object. I haven’t gotten the help I need. Some people in this forum haven’t been of help, and instead they’ve acted very arrogant and condescending. I am new to these, and all i need is help in the form of code and explanation.
Here is what I am working on:
I am tying to 1. loop through the array and print out just the question 2. loop through array and print out the just the answer. 3. Printout the answer and the question

var myQuestions = [
	{
		question: "What is 10/2?",
		answers: {
			a: '3',
			b: '5',
			c: '115'
		},
		correctAnswer: 'b'
	},
	{
		question: "What is 30/3?",
		answers: {
			a: '3',
			b: '5',
			c: '10'
		},
		correctAnswer: 'c'
	}
];

my proposed solution:

for(var i = 0, i < myQuestions.length; i++){
	console.log(myQuestions[question])
}

maybe they have not had the chance to log back in the forum?

also people get a notification only if you reply directly to their post or mention them, otherwise they don’t know you have answered


do you want to get the question and the answer of one single question? or all the questions and then all the answers?

1 Like

you do not have a question variable, you can’t do this
also, if you are using a loop you should think how to use the i variable

This is a great way to learn about objects, and about arrays of objects, but from the questions you are asking it seems your understanding of some earlier steps in the javascript language are in question.

Your loop is looking better, which is great (yes, you needed both an ending condition and one that won’t error). But have you learned how to read the properties of an object? For example:

const questionObj = {
  question: "What is 10/2?",
  answers: {
    a: '3',
    b: '5',
    c: '115'
  },
  correctAnswer: 'b'
};

This is not an array of objects, it is just one object. Can you tell me how you would access just that one object’s question? Answer? How would you display the correct answer value? (That third is a curve ball, and extra credit).

When you can comfortably address that, then doing the same thing for an array of those same objects becomes a little less challenging - rather than using questionObj, you might use questions[i], but everything else would be just the same.

As to the helpfulness of this community, if you take the time to read through the forums, you might notice that there are quite a few of us who devote quite a lot of time to doing just this - helping out, guiding learners, and taking the time to do so in a way that fosters learning and challenges the learner. We are always willing to help, around the other things we have going on, because of course we all have lives outside of this.

But… Nobody in this forum is going to hand you a guitar (assuming you’ve never played a guitar), and show you how to jam out like Santana. Nobody in this forum has a magic mouse that is going to show you how to code with arrays of objects and have you simply get it, without taking the time to learn the basics. It takes time, and dedication, and commitment. Both on your part to research and learn, and our part to suggest smaller building blocks first.

You may not like the response you got, and for that I’m deeply sorry, but the fact is, you were given help. It may not have felt like help, but from the outside, it was a clear direction for you to consider.

2 Likes