Skip to content

Instantly share code, notes, and snippets.

@laran
Last active October 31, 2018 17:00
Show Gist options
  • Save laran/128fd95420b533dd9aae79e6c12e1302 to your computer and use it in GitHub Desktop.
Save laran/128fd95420b533dd9aae79e6c12e1302 to your computer and use it in GitHub Desktop.
Iterate attributes in object
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