![]() |
VOOZH | about |
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.
Object.entries(obj);Object.entries() returns an array consisting of enumerable property [key, value] pairs of the object passed.
Example 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.
[ '1', 'billy' ]
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.
[ [ '10', 'adam' ], [ '35', 'chris' ], [ '200', 'billy' ] ]
You can easily iterate over the object’s properties by using Object.entries() in combination with array methods like forEach() or map().
name: Alice age: 25 city: New York
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.
[ [ 'name', 'Alice' ], [ 'age', 25 ], [ 'city', 'New York' ] ]
You can use Object.entries() to manipulate or transform an object, such as changing its structure or reformatting it.
[ [ 'NAME', 'Alice' ], [ 'AGE', 25 ], [ 'CITY', 'New York' ] ]
Since arrays are objects in JavaScript, Object.entries() can be applied to arrays to get index-value pairs.
[ [ '0', 'Apple' ], [ '1', 'Banana' ], [ '2', 'Cherry' ] ]
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.
[ [ 'name', 'Alice' ] ]