JavaScript entries()
The entries()
method is a built-in method in JavaScript that is used to return an array of key-value pairs from an object.
Syntax
The syntax for the entries()
method is as follows:
object.entries()
Here, object
is the object from which you want to retrieve the key-value pairs.
Example
Consider the following object:
const car = {
make: 'Honda',
model: 'Civic',
year: 2021
};
To get the key-value pairs from this object, you can use the entries()
method as follows:
const entries = Object.entries(car);
console.log(entries);
This will output:
[ [ 'make', 'Honda' ], [ 'model', 'Civic' ], [ 'year', 2021 ] ]
Output
The entries()
method returns an array of arrays. Each nested array contains two elements, the first element being the key and the second element being the corresponding value.