Last Updated : 18 Sep, 2024
The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed.
Syntax:Object.entries(obj);Parameters:
Object.entries() returns an array consisting of enumerable property [key, value] pairs of the object passed.
Key CharacteristicsExample 1: In this example, an object "obj" has been created with three property[key, value] pairs, and the Object.entries() method is used to return the first property [key, value] pair of the object.
javascript
// Creating an object constructor
// and assigning values to it
const obj = { 0: 'adam', 1: 'billy', 2: 'chris' };
// Displaying the enumerable property [key, value]
// pairs of the object using object.entries() method
console.log(Object.entries(obj)[1]);
Example 2: In this example, an object "obj" has been created with three property[key, value] pairs, and the Object.entries() method is used to return all the property [key, value] pairs of the object.
javascript
// Creating an object constructor and
// assigning values to it
const obj = { 10: 'adam', 200: 'billy', 35: 'chris' };
// Displaying the enumerable property [key, value]
// pairs of the object using object.entries() method
console.log(Object.entries(obj));
[ [ '10', 'adam' ], [ '35', 'chris' ], [ '200', 'billy' ] ]Applications of Object.entries() Iterating over Objects:
You can easily iterate over the object’s properties by using Object.entries() in combination with array methods like forEach() or map().
JavaScript
const obj = { name: 'Alice', age: 25, city: 'New York' };
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key}: ${value}`);
});
name: Alice age: 25 city: New YorkConverting Objects to Arrays:
Since Object.entries() returns an array of arrays, it is useful for converting objects into arrays, especially when you need to work with array-specific functions.
JavaScript
const obj = { name: 'Alice', age: 25, city: 'New York' };
const entries = Object.entries(obj);
console.log(entries);
[ [ 'name', 'Alice' ], [ 'age', 25 ], [ 'city', 'New York' ] ]Transforming Objects:
You can use Object.entries() to manipulate or transform an object, such as changing its structure or reformatting it.
JavaScript
const obj = { name: 'Alice', age: 25, city: 'New York' };
const transformed = Object.entries(obj).map(([key, value]) => [key.toUpperCase(), value]);
console.log(transformed);
[ [ 'NAME', 'Alice' ], [ 'AGE', 25 ], [ 'CITY', 'New York' ] ]Handling Arrays:
Since arrays are objects in JavaScript, Object.entries() can be applied to arrays to get index-value pairs.
JavaScript
const arr = ['Apple', 'Banana', 'Cherry'];
const arrEntries = Object.entries(arr);
console.log(arrEntries);
[ [ '0', 'Apple' ], [ '1', 'Banana' ], [ '2', 'Cherry' ] ]Handling Non-Enumerable Properties
The Object.entries() method only includes enumerable properties in the returned array. Non-enumerable properties (properties defined with Object.defineProperty() and the enumerable: false option) will not be included.
JavaScript
const obj = { name: 'Alice' };
Object.defineProperty(obj, 'age', {
value: 25,
enumerable: false
});
console.log(Object.entries(obj));
[ [ 'name', 'Alice' ] ]Supported Browsers:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4