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
Object.entries()
is an ECMAScript 2017 feature.
JavaScript 2017 is supported in all modern browsers since September 2017:
Chrome 58 | Edge 15 | Firefox 52 | Safari 11 | Opera 45 |
Apr 2017 | Apr 2017 | Mar 2017 | Sep 2017 | May 2017 |
Object.entries()
is not supported in Internet Explorer.