Getting Properties Of All Objects In Array | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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