The Object.entries() method returns an array of key-value pairs of an object's enumerable properties.
Example
const obj = { name: "Adam", age: 20, location: "Nepal" };
// returns properties in key-value format
console.log(Object.entries(obj));
// Output: [ [ 'name', 'Adam' ], [ 'age', 20 ], [ 'location', 'Nepal' ] ]
entries() Syntax
The syntax of the entries() method is:
Object.entries(obj)
The entries() method, being a static method, is called using the Object class name.
entries() Parameters
The entries() method takes in:
- obj - the object whose enumerable properties are to be returned.
entries() Return Value
The entries() method returns an array of all the enumerable properties of an object, where each element will be a key-value pair.
Example 1: JavaScript Object.entries()
let student= {
name: "Lida",
age: 21
};
// convert student's properties
// into an array of key-value pairs
let entries = Object.entries(student);
console.log(entries);
// Output:[ [ 'name', 'Lida' ], [ 'age', 21 ] ]
In the above example, we have created an object named student. Then, we used the entries() method to get its enumerable properties in key-value format.
Note: Enumerable properties are those properties that are visible in