Getting Properties Of All Objects In Array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Getting Properties Of All Objects In Array

I was wondering how I can get the properties of objects that are in an array. I wanted to go through the array in a loop and output their properties, but the browser just outputs undefined. I’m assuming this is because it doesn’t know they are all of the same class and therefore all have those properties, so how can I tell it this?

23rd Nov 2019, 8:36 PM
Gisele Nelson
Gisele Nelson - avatar
3 Answers
+ 1
I think I just figured it out. I’m used to java where you can use a for each (for/in) for going through arrays, but from what I read, it sounds like in javascript, they only work for looping through object properties. Thanks for your help though!
23rd Nov 2019, 10:04 PM
Gisele Nelson
Gisele Nelson - avatar
0
As long as all the objects in the class actually do have that property, it shouldn’t be an issue. Can you share the code you’re having issues with?
23rd Nov 2019, 9:22 PM
Jax
Jax - avatar
0
The part that keeps showing undefined is for(var x in lettersInHand) document.getElementById(“hand”). innerHTML+=‘<span>’+x.character+’<span’; I put a print earlier in the method and it showed everything in the array as being a Letter, so I don’t know why they don’t seem to have a character property in the loop
23rd Nov 2019, 9:58 PM
Gisele Nelson
Gisele Nelson - avatar