DEV Community

Priyanshi Jain
Priyanshi Jain

Posted on

How to access Objects?

As we can't access data from Object directly or perform operations on that.Firstly we have to convert the object in an array format then only we can apply functions .

In JavaScript, you can access the properties and values of an object in several ways. Here are the most common methods:

1 . Dot Notation ()

_the popular one _
Ex:
const person = {
name: "John",
};

console.log(person.name); // Output: John

2 . Bracket Notation
(Suitable for when object contain spacing in key )

const person ={
first name='Panchi';
}
console.log(person['first name']);

  1. Accessing All Keys and Values [ IMP] You can use Object.keys(), Object.values(), or Object.entries() to get all keys, values, or both

const person = {
name: "John",
age: 30,
city: "New York"
};

console.log(Object.keys(person)); // Output: ["name", "age", "city"]
console.log(Object.values(person)); // Output: ["John", 30, "New York"]
console.log(Object.entries(person));// Output: [["name", "John"], ["age", 30], ["city", "New York"]]

** NOTE : Object.entries used to return array then on that resulting array we can apply like .map() or .filter function.**
Enter fullscreen mode Exit fullscreen mode

Moreover Resulting array contain data like enum in key value pair

Top comments (0)