ES6 - Object keys values entries static methods

ES6 JavaScript

In this ES6 tutorial we are going to learn about Object static methods like keys, values and entries.

Let's say we have the following object with some properties.

const user = {
  username: 'yusufshakeel',
  isOnline: true,
  score: 10,
  lastThreePoints: [6,7,8],
  profiles: {
    youtube: 'https://www.youtube.com/@yusufshakeel'
  }
};

Get the keys in the object

To get all the keys in the object we use the Object.keys() static method.

const keys = Object.keys(user);
console.log(keys);

Get the values in the object

To get all the values of the keys in the object we use the Object.values() static method.

const values = Object.values(user);
console.log(values);

Print all the key-value pairs in the object

To print all the key-value pairs in the object we use the Object.entries static method.

const entries = Object.entries(user);
console.log(entries);

Alright this brings us to the end of another tutorial. See you soon. Have fun coding.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT