formations/javascript/base/objets-2.md

32 lines
457 B
Markdown
Raw Permalink Normal View History

2015-03-26 17:36:34 +01:00
# .cadoles-slide-title[Objets (2/2)]
**Itération sur les propriétés**
```js
var obj = {
pro1: 1,
prop2: 'test',
prop3: {}
};
// Boucle for..in
var value;
for(var key in obj) {
if( obj.hasOwnProperty(key) ) {
value = obj[key];
console.log('Key: ', key, 'Value:', value);
}
}
// Méthode Object.keys()
Object.keys(obj)
.forEach(function(key) {
var value = obj[key];
console.log('Key: ', key, 'Value:', value);
})
;
```