- Lil'bots
- Learn JavaScript
- JavaScript Objects
- The Object.entries() Method
The Object.entries() Method
The Object.entries()
method in JavaScript is a powerful and versatile tool that allows you to convert objects into arrays of key-value pairs. This can be incredibly useful for various tasks such as iterating over an object's properties, converting an object to a Map, or simply making object property manipulation more straightforward.
Syntax
Object.entries(obj)
Parameters
- obj: The object whose enumerable string-keyed property key-value pairs are to be returned.
Return Value
An array of the given object's own enumerable string-keyed property key-value pairs.
Basic Usage
Let's start with a simple example to see how Object.entries()
works:
const obj = { foo: "bar", baz: 42 }; console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
The method converts the object into an array of key-value pairs. Each pair is represented as an array of two elements: the key and the value.
Working with Array-like Objects
Object.entries()
can also be used with array-like objects:
const arrayLike = { 0: "a", 1: "b", 2: "c" }; console.log(Object.entries(arrayLike)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
Even if the keys are numeric, they will still be treated as strings within the key-value pairs.
Key Order
Keys in JavaScript objects do not always maintain the order you expect. However, Object.entries()
respects the order:
const unordered = { 100: "a", 2: "b", 7: "c" }; console.log(Object.entries(unordered)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]
Handling Non-Enumerable Properties
Non-enumerable properties will be skipped:
const myObj = Object.create( {}, { getFoo: { value() { return this.foo; }, }, } ); myObj.foo = "bar"; console.log(Object.entries(myObj)); // [ ['foo', 'bar'] ]
Primitives and Errors
Non-object arguments are coerced to objects, but undefined
and null
throw a TypeError
:
console.log(Object.entries("foo")); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ] console.log(Object.entries(100)); // [] // console.log(Object.entries(null)); // TypeError: Cannot convert undefined or null to object
Converting to a Map
Objects can be easily converted to a Map:
const obj = { foo: "bar", baz: 42 }; const map = new Map(Object.entries(obj)); console.log(map); // Map(2) {"foo" => "bar", "baz" => 42}
Iterating Through Objects
You can iterate through an object's properties easily using Object.entries()
with for...of
loop or array methods:
const obj = { a: 5, b: 7, c: 9 }; // Using for...of loop for (const [key, value] of Object.entries(obj)) { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" } // Using .forEach method Object.entries(obj).forEach(([key, value]) => { console.log(`${key} ${value}`); // "a 5", "b 7", "c 9" });
Start Building in JavaScript Today
Lilbots is the best platform for creating powerful JavaScript bots
- Built in access to cutting edge AI models
- Out of the box integration with hundreds of APIs
- Collaborate with coding workspaces
- Discover and fork thousands of bots built by the community