The Object.entries()
method, introduced in ES8 (ECMAScript 2017), returns an array of a given object's own enumerable string-keyed property [key, value]
pairs. This is incredibly useful for iterating over object properties or for converting objects into a Map.
Example 1: Basic Usage
const person = {
firstName: 'John',
lastName: 'Doe',
age: 30
};
// Using Object.entries() to get an array of [key, value] pairs
const entries = Object.entries(person);
console.log(entries);
// Expected output: [ ['firstName', 'John'], ['lastName', 'Doe'], ['age', 30] ]
Explanation
This example demonstrates the fundamental use of Object.entries()
. It takes the person
object and returns an array where each inner array contains a key and its corresponding value.
Example 2: Iterating with for...of loop
const user = {
username: 'coder123',
email: 'coder123@example.com',
isLoggedIn: true
};
// Looping through the entries of the user object
for (const [key, value] of Object.entries(user)) {
console.log(`Key: ${key}, Value: ${value}`);
}
Explanation
Here, Object.entries()
is used within a for...of
loop to conveniently access both the key and value of each property in the user
object. This is a common and readable way to iterate over object data.
Example 3: Converting an Object to a Map
const car = {
make: 'Honda',
model: 'Civic',
year: 2022
};
// Creating a new Map from the car object
const carMap = new Map(Object.entries(car));
console.log(carMap.get('model')); // Expected output: Civic
Explanation
This code shows how Object.entries()
can be used to convert an object into a Map
. The Map
constructor accepts an iterable of [key, value]
pairs, which is exactly what Object.entries()
provides.
Example 4: Filtering Object Properties
const product = {
name: 'Laptop',
price: 1200,
inStock: true,
rating: 4.5
};
// Filtering for properties where the value is a number
const numericProperties = Object.entries(product).filter(([key, value]) => typeof value === 'number');
console.log(numericProperties);
// Expected output: [ ['price', 1200], ['rating', 4.5] ]
Explanation
This example demonstrates how to filter the properties of an object. Object.entries()
creates the array, and then the filter
method is used to create a new array containing only the properties with numeric values.
Example 5: Handling Objects with symbol properties
const id = Symbol('id');
const employee = {
name: 'Jane',
[id]: 12345
};
// Object.entries() only includes string-keyed properties
const employeeEntries = Object.entries(employee);
console.log(employeeEntries); // Expected output: [ ['name', 'Jane'] ]
Explanation
This code illustrates that Object.entries()
ignores properties with Symbol keys. Only the name
property, which has a string key, is included in the resulting array.
Example 6: Using with Array-like Objects
const arrayLike = {
0: 'a',
1: 'b',
2: 'c'
};
// Using Object.entries() on an array-like object
const entries = Object.entries(arrayLike);
console.log(entries);
// Expected output: [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
Explanation
This example shows that Object.entries()
works with array-like objects. The numeric keys are converted to strings in the resulting array of [key, value]
pairs.
Example 7: Transforming Keys to Uppercase
const settings = {
theme: 'dark',
fontSize: 16,
notifications: true
};
// Creating a new object with uppercase keys
const upperCaseSettings = Object.fromEntries(
Object.entries(settings).map(([key, value]) => [key.toUpperCase(), value])
);
console.log(upperCaseSettings);
// Expected output: { THEME: 'dark', FONTSIZE: 16, NOTIFICATIONS: true }
Explanation
This example combines Object.entries()
with map
and Object.fromEntries()
to transform an object. It gets the [key, value]
pairs, converts each key to uppercase, and then creates a new object from the transformed pairs.