How to get the key-value pairs of an object using the Object.entries function?

WBOY
Release: 2023-11-18 12:14:23
Original
724 people have browsed it

How to get the key-value pairs of an object using the Object.entries function?

How to use the Object.entries function to get the key-value pair of an object?

In JavaScript programming, we often need to operate on the key-value pairs of objects. The Object.entries method introduced in ES2017 can help us quickly obtain the key-value pairs of objects. This article will introduce the usage of Object.entries, with specific code examples.

1. Basic usage of Object.entries
The Object.entries function receives an object as a parameter and returns a two-dimensional array containing the key-value pair of the object. where each array element is a small array containing a key and a value.

For example, we have an object:
const person = {
name: "John",
age: 30,
gender: "male"
};

If we want to get the key-value pair of the person object, we can use the Object.entries function:
const entries = Object.entries(person);

After executing the above code, the value of entries will be a two-dimensional array containing all key-value pairs of the person object:
[
["name", "John"],
["age", 30],
["gender" , "male"]
]

2. Traverse key-value pairs
After obtaining the key-value pairs of the object, we can use the for...of loop to traverse them, or use the forEach method. traverse.

  1. Use for...of loop traversal
    The for...of loop is a new feature introduced in ES6, which can easily traverse arrays or array-like objects.

Then we take the person object as an example and use the for...of loop to traverse the key-value pairs of the person object:
for (const [key, value] of Object.entries(person) ) {
console.log(${key}: ${value});
}

After executing the above code, the console will output:
name : John
age: 30
gender: male

  1. Use forEach method to traverse
    In addition to the for...of loop, we can also use the forEach method of the array to Object. The array returned by entries is traversed.

Also taking the person object as an example, use the forEach method to traverse the key-value pairs of the person object:
Object.entries(person).forEach(([key, value]) => {
console.log(${key}: ${value});
});

After executing the above code, the console will output the same result:
name: John
age: 30
gender: male

3. Use Object.entries to clone the object
The Object.entries function can not only obtain the key-value pair of the object, but also It can help us achieve object cloning.

We can convert a two-dimensional array containing key-value pairs into a new object by combining the Object.fromEntries method.

For example, we have a person object:
const person = {
name: "John",
age: 30,
gender: "male"
};

Now, we want to create a new object with the same content as the person object by cloning. We can use Object.entries and Object.fromEntries to accomplish this task:
const clonedPerson = Object.fromEntries(Object.entries(person));

After executing the above code, clonedPerson will be a new object , its content is exactly the same as the person object.

Summary
The Object.entries function is a new method introduced in ES2017, which can help us quickly obtain the key-value pairs of objects. By using a for...of loop or the forEach method of an array, we can easily iterate through these key-value pairs. In addition, combined with the Object.fromEntries method, we can also clone objects through Object.entries.

I hope this article will help you understand the usage of the Object.entries function. In daily JavaScript programming, proper use of Object.entries can make it easier to operate the key-value pairs of objects.

The above is the detailed content of How to get the key-value pairs of an object using the Object.entries function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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