ES 6: Using Object.entries() to iterate the keys in the Object

cchunduri

Chaitanya Chunduri

Posted on July 25, 2019

ES 6: Using Object.entries() to iterate the keys in the Object

You can iterate keys in an object using ES 6 like below.

let employe = {
 name: 'Chaitanya',
 eno: '1'
};

for (let [key, value] of Object.entries(employe)) {
  console.log(`${key}: ${value}`);
}
Enter fullscreen mode Exit fullscreen mode

or there is another way using forEach method

Object.entries(employe).forEach(([key, value]) => {
  console.log(`${key}: ${value}`); 
});
Enter fullscreen mode Exit fullscreen mode

Object.enttries() got standardised in ECMAScript2017. Below is the browser support chart collected from MDN

💖 💪 🙅 🚩
cchunduri
Chaitanya Chunduri

Posted on July 25, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related