"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to access Objects?

How to access Objects?

Published on 2024-11-03
Browse:608

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.**

Moreover Resulting array contain data like enum in key value pair

Release Statement This article is reproduced at: https://dev.to/priyanshi_jain_63d791d47a/how-to-access-objects-19kj?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3