Home  >  Article  >  Web Front-end  >  How to access Objects?

How to access Objects?

王林
王林Original
2024-08-29 10:36:23527browse

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

The above is the detailed content of How to access Objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn