Home >Web Front-end >JS Tutorial >How to use Object.entries() method in js? (code example)

How to use Object.entries() method in js? (code example)

青灯夜游
青灯夜游Original
2018-12-28 11:16:417129browse

The Object.entries() method can take an object as a parameter, use the enumerable attribute [key, value] pairs on this object as elements of the array, and then return this string array. Let's take a closer look at how to use the Object.entries() method. [Recommended related video tutorials: JavaScript Tutorial]

How to use Object.entries() method in js? (code example)

##Basic syntax:

Object.entries(obj)

Description:

1. obj: represents an object containing enumerable attribute [key, value] pairs

2. The order of the attributes in the output array and The object's properties are given in the same order as in the for...in loop.

Usage of Object.entries() method

Let’s take a simple example to see how to use the Object.entries() method.

Example 1: All [key, value] pairs that can be listed by the Object.entries() method.

var obj ={ name: '小明', age: 20, sex: '男' };
console.log(Object.entries(obj));

Description: In this example, an object "obj" is created using three attribute [key, value] pairs, and all attributes of the object [key, value] are returned using the Object.entries() method ]right.

Output:

How to use Object.entries() method in js? (code example)

Example 2: The Object.entries() method can list the specified properties in the object.

var obj ={ name: '小明', age: 20, sex: '男' };
console.log(Object.entries(obj)[0]);
console.log(Object.entries(obj)[2]);

Description: In this example, use the Object.entries() method to return the 1st and 3rd attribute [key, value] pairs of the object.

Output:

How to use Object.entries() method in js? (code example)

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

The above is the detailed content of How to use Object.entries() method in js? (code example). 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