JavaScript(JS) object method - entries
The Object.entries()
method is a built-in method in JavaScript that returns an array of an object's own enumerable property [key, value] pairs, in the same order as a for...in loop. This method returns an array of arrays, where each sub-array contains two elements: the property key and its corresponding value.
Here's the syntax for using Object.entries()
:
Object.entries(obj)Source:www.theitroad.com
Where obj
is the object whose own enumerable property [key, value] pairs you want to retrieve.
Here's an example:
const person = { name: 'John', age: 30, city: 'New York' }; const entries = Object.entries(person); console.log(entries); // Output: [ ["name", "John"], ["age", 30], ["city", "New York"] ]
In this example, we create an object called person
with three properties: name
, age
, and city
. We then call Object.entries()
on the person
object, which returns an array of arrays containing the property [key, value] pairs. Finally, we log the array to the console.
Note that the order of the property [key, value] pairs in the resulting array is the same as the order in which they were defined in the object. Also, Object.entries()
only retrieves own enumerable properties of an object, which means that it doesn't retrieve inherited properties or non-enumerable properties.