Home >Web Front-end >JS Tutorial >How to use Object.entries() method in js? (code example)
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]
##Basic syntax:
Object.entries(obj)Description: 1. obj: represents an object containing enumerable attribute [key, value] pairs2. 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: 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: 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!