How to Access an Object’s Keys, Entries and Values in JavaScript

How to Access an Object’s Keys, Entries and Values in JavaScript

Accessing an Object's Keys, Values, and Entries in JavaScript. Object.keys(obj) → returns an array of a given object's property names. Object.values(obj) → returns an array of a given object's own property values. Object.entries(obj) → returns an array of a given object's own string-keyed property [key, value] pairs.

Accessing an Object’s Keys, Values, and Entries in JavaScript. Let’s dive into objects

  • Object.keys(obj) → returns an array of a given object’s property names.
  • Object.values(obj) → returns an array of a given object's own property values.
  • Object.entries(obj) → returns an array of a given object’s own string-keyed property [key, value] pairs.
var user = {

    name : "John",

    profession : "👨‍⚕️",

    salary  : 10000

}

Object.keys(user); // ["name", "profession", "salary"]

Object.values(user); // ["John", "👨‍⚕️", 10000]

Object.entries(user); 

//output
0: (2) ["name", "John"]
1: (2) [“profession”, “👨‍⚕️”]
2: (2) [“salary”, “10000]
length: 3

All the above three methods return only the enumerable property of the object.

var user = {
    name : "John"
}

Object.defineProperty( user , 'salary', {
   value: 10000,
   enumerable: false
});

In the above code, we define a property salary and set enumerable property as false.This will make the salary hidden from the Object.keys.

Object.keys(user); ["name"]

Object.values(user); ["John"]

Object.entries(user); //  ["name", "John"]

We cannot loop through the symbol properties of the object.

var user = {
    name : "John"
}

let Passcode = Symbol("Passcode");

user[Passcode] = "1234";

Now we have set the symbol as a property of the object, but this will not be accessible using the three methods above.

Object.keys(user); //["name"]

Object.values(user); // ["John"]

Object.entries(user); //  ["name", "John"]

To access the symbol property of the object, we can use getOwnPropertySymbols().

Object.getOwnPropertySymbols(user); //[Symbol(Passcode)]

We can use for… as an alternative to the above methods.

var user = {
    name : "John",
    age  : 25
}

for(const property in user) {
    console.log(`user[${property}] = ${user[property]}`);
}

//output

user[name] = John
user[age]  = 25

Using map with Object.entries.

var user = {
    name : "John",
    age  : 25
}

let entries = Object.entries(user);

entries.map( ([prop, val]) => console.log(prop, val));

Alternatively, we can also use forEach.

var user = {
    name : "John",
    age  : 25
}

let entries = Object.entries(user);

entries.forEach( ([prop, val]) => console.log(prop, val));

Using for…of with Object.entries:

var user = {
    name : "John",
    age  : 25
}

let entries = Object.entries(user);

for(const [prop, val] of entries) {
    console.log(prop, val);
}

Thank you for reading ! I hope this tutorial will surely help and you if you liked this tutorial, please consider sharing it with others.

JavaScript Tips React Front End Development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

Hire Dedicated React Native Developer in India | React Native Development

Hire dedicated React Native developers for your next project. As the top react native development company we offer you the best service as per your business needs.

Hire React Js Developer from Expert React JS Development Company

Are you looking to [hire React JS developer](https://www.mobiwebtech.com/react-js-development-company/ "hire React JS developer") from a prestigious and reliable React JS development company? Visit at Mobiweb Technologies here we have a big team...

Top 20 VSCode Extensions For Every Front-End Developer

Visual Studio Code(VS Code) is one of the most popular and most loved IDE used among developers these days. I did a lot of googling and searching for the best extensions, and I thought I’ll sum up all of them and write a post about it, so that it’ll be helpful for the developer community. In this post I’ll be sharing with you all the nice-to-have extensions in order to make development quick and easy! In this post, you'll see Top 20 VSCode Extensions For Every Front-End Developer