Skip to content

Instantly share code, notes, and snippets.

@imparvez
Created September 22, 2017 12:05
Show Gist options
  • Save imparvez/fedcf666718d29f58ac3bebc0f198560 to your computer and use it in GitHub Desktop.
Save imparvez/fedcf666718d29f58ac3bebc0f198560 to your computer and use it in GitHub Desktop.
// Different ways to loop or iterate objects in JavaScript
var newObjects = {
'p1': 'item 1',
'p2': 'item 2',
'p3': 'item 3',
}
// Way no: 1
for(var key in newObjects) {
if(newObjects.hasOwnProperty(key)) {
console.log(key +" => "+ newObjects[key]);
}
}
// Way no: 2
Object.keys(newObjects).forEach(function(key){
console.log(key + ' => ' + newObjects[key]);
})
// Way no: 3
Object.entries(newObjects).forEach(function([key, value]){
console.log(key+" => "+value)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment