JS - Boucler sur un objet

Nous souhaitons ici boucler sur l'objet suivant :

var hash = {"key1": "value1", "key2": "value2"};
                
hash["key3"] = "value3";

Pour ce faire, il existe deux solutions.

La première :

Object.keys(hash).forEach(function (key) { 
    var value = hash[key]
    console.log(key + " => " + value);
})

La seconde :

for (var key in hash) {
        console.log(key + " => " + hash[key]);
}

 


2 août 2018 - Wakonda - JavaScript

Rechercher

Tags

Publicité

Suivez-nous

Aidez-nous !


Loading…
Loading the web debug toolbar…
Attempt #