How do I enumerate the properties of a JavaScript object

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

   This question already has an answer here:
    * /questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object /questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object 23 answers

Answers

Simple enough:

for(var propertyName in myObject) {
   // propertyName is what you want
   // you can get the value like this: myObject[propertyName]
}

Now, you will not get private variables this way because they are not available.


http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object#86306 http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object#86306

Now, that said, hasOwnProperty() is useful for filtering, but we don t need to sound a warning as if there is something dangerous in getting inherited properties.

http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object#86306 http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object#86306

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils