Last active
October 31, 2018 17:00
-
-
Save laran/128fd95420b533dd9aae79e6c12e1302 to your computer and use it in GitHub Desktop.
Iterate attributes in object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let thing = { attr1: 'val1', attr2: 'val2' }; | |
// Least nice | |
let keys = Object.keys(thing); | |
for(let i = 0; i < keys.length; i++) { | |
console.log(thing[keys[i]]); | |
} | |
// Nicer | |
Object.keys(thing).forEach((attribute) => { | |
console.log(thing[attribute]); | |
}); | |
// Nicest | |
for(attribute in thing) { | |
console.log(thing[attribute]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment