Skip to main content
Add combined version with Object.entries, for-of, and destructuring.
Source Link
T.J. Crowder
  • 1.1m
  • 201
  • 2k
  • 2k

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

You can combine for...of, destructuring, and Object.entries:

for (const [key, value] of Object.entries(obj)) { console.log(key, value); } 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

You can combine for...of, destructuring, and Object.entries:

for (const [key, value] of Object.entries(obj)) { console.log(key, value); } 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Political view on their new unfavoured naming convention
Source Link

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ES6ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ES2017ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Edit: ES2016 → ES6

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ES6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ES2017 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Edit: ES2016 → ES6

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ECMAScript 6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ECMAScript 8 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

deleted 3 characters in body
Source Link
Axel Rauschmayer
  • 25.8k
  • 5
  • 26
  • 17

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ES2016ES6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ES2017 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Edit: ES2016 → ES6

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ES2016 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ES2017 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Under ECMAScript 5, you can combine Object.keys() and Array.prototype.forEach():

var obj = { first: "John", last: "Doe" }; Object.keys(obj).forEach(function(key) { console.log(key, obj[key]); }); 

ES6 adds for...of:

for (const key of Object.keys(obj)) { console.log(key, obj[key]); } 

ES2017 adds Object.entries() which avoids having to look up each value in the original object:

Object.entries(obj).forEach( ([key, value]) => console.log(key, value) ); 

Both Object.keys() and Object.entries() iterate properties in the same order as a for...in loop but ignore the prototype chain. Only the object's own enumerable properties are iterated.

Edit: ES2016 → ES6

Add ES2016 to the chronology
Source Link
Dan Dascalescu
  • 153.6k
  • 66
  • 336
  • 422
Loading
add Object.entries() and MDN links
Source Link
David Harkness
  • 36.6k
  • 11
  • 119
  • 136
Loading
Rollback to Revision 3
Source Link
user229044
  • 240.9k
  • 41
  • 347
  • 350
Loading
added 211 characters in body
Source Link
Evan Carroll
  • 1
  • 53
  • 298
  • 516
Loading
backticks
Source Link
David Harkness
  • 36.6k
  • 11
  • 119
  • 136
Loading
deleted 81 characters in body
Source Link
Axel Rauschmayer
  • 25.8k
  • 5
  • 26
  • 17
Loading
Source Link
Axel Rauschmayer
  • 25.8k
  • 5
  • 26
  • 17
Loading