JavaScript Object.entries()
Examples
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
let text = Object.entries(person);
Try it Yourself »
Object.entries() makes it simpler to use objects in loops:
const fruits = {Bananas:300, Oranges:200, Apples:500};
let text = "";
for (let [fruit, value] of Object.entries(fruits)) {
text += fruit + ": " + value + "<br>";
}
Try it Yourself »
More examples below.
Description
The Object.entries()
method returns an array of the key/value pairs of an object.
The Object.entries()
method does not change the original object.
Related Methods:
Object.keys() returns the keys (properties) of any object type.
Object.values() returns the values of all object keys (properties).
Object.entries() returns the keys and values of any object types.
The methods above return an Iterable (enumerable array).
Iterables makes it simpler to use objects in loops and to convert objects into maps.
Syntax
Object.values(object)
Parameters
Parameter | Description |
object | Optional. An object. |
Return Value
Type | Description |
Array | An iterable array of the object's key/value pairs. |
More Examples
Object.entries()
makes it simpler to convert objects to maps:
Example
const fruits = {Bananas:300, Oranges:200, Apples:500};
const myMap = new Map(Object.entries(fruits));
Try it Yourself »
Browser Support
ECMAScript 2017 added the Object.entries()
method to objects.
Object.entries()
is supported in all modern browsers since March 2017:
Chrome 47 | Edge 14 | Firefox 47 | Safari 10.1 | Opera 41 |
Jun 2016 | Aug 2016 | Jun 2016 | Mar 2017 | Oct 2016 |