Object.entries()
The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value]
pairs, in the same order as that provided by a for...in
loop. The only difference is that a for...in
loop enumerates properties in the prototype chain, while Object.entries()
returns only own enumerable properties of the object. Object.entries()
was introduced in ECMAScript 2017
Syntax
Object.entries(obj);
Parameter
obj
: The object whose enumerable own property[key, value]
pairs are to be returned.
Return Value
An array of [key, value]
pairs for the given object's enumerable own properties.
Examples
Example 1: Basic usage
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
};
const entries = Object.entries(person);
console.log(entries);
// Output: [["firstName", "John"], ["lastName", "Doe"], ["age", 30]]
Example 2: With for...of loop
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
};
for (const [key, value] of Object.entries(person)) {
console.log(`${key}: ${value}`);
}
// Output:
// firstName: John
// lastName: Doe
// age: 30
Example 3
const arr = ["a", "b", "c"];
const entries = Object.entries(arr);
console.log(entries); // Output: [["0", "a"], ["1", "b"], ["2", "c"]]
Notes
-
The order of the
[key, value]
pairs returned byObject.entries()
is the same as that provided by afor...in
loop. However, unlike afor...in
loop,Object.entries()
only returns own enumerable properties of the object, not those inherited from the prototype chain. -
If the input object is
null
orundefined
,Object.entries()
will throw aTypeError
. -
If the input object is not an object (i.e. a primitive value such as a string, number, or boolean),
Object.entries()
will first coerce it into an object using theToObject()
operation (i.e.Object("foo")
will return Object{[[PrimitiveValue]]: "foo"}
), then proceed with returning an array of[key, value]
pairs for the object's enumerable own properties. -
Object.entries()
was introduced in ECMAScript 2017 and is not supported by older browsers such as Internet Explorer. -
The entries in the resulting array are also affected by the object's properties that have non-enumerable flags set to
true
.
Exercises
-
Given an object
person
with propertiesname
,age
, andjob
, useObject.entries()
to iterate over the object and log each property and its value to the console. -
Given an object
student
with propertiesname
,major
,gpa
, andgradYear
, useObject.entries()
to create a new object containing only the propertiesname
andgpa
, and log the new object to the console. -
Given an object car with properties
brand
,model
,year
, andcolor
, useObject.entries()
to iterate over the object and log each property and its value to the console in the format<property>
:<value>
.
Solutions
Exercise 1
const person = {
name: 'Alice',
age: 30,
job: 'Software Engineer'
};
const entries = Object.entries(person);
for (const [key, value] of entries) {
console.log(`${key}: ${value}`);
}
// Output:
// name: Alice
// age: 30
// job: Software Engineer
In this solution, we first use Object.entries()
to convert the person
object into an array of key-value pairs. We then use a for...of
loop to iterate over the entries array and log each key-value pair to the console.
Exercise 2
const student = {
name: 'Bob',
major: 'Computer Science',
gpa: 3.7,
gradYear: 2022
};
const entries = Object.entries(student);
const newStudent = {};
for (const [key, value] of entries) {
if (key === 'name' || key === 'gpa') {
newStudent[key] = value;
}
}
console.log(newStudent);
// Output: { name: 'Bob', gpa: 3.7 }
In this solution, we first use Object.entries()
to convert the student
object into an array of key-value pairs. We then create a new empty object called newStudent
. We use a for...of
loop to iterate over the entries array and check if the key is either 'name
' or 'gpa
'. If the key is one of these properties, we add the key-value pair to the newStudent
object. Finally, we log the newStudent
object to the console.
Exercise 3
const car = {
brand: 'Ford',
model: 'Mustang',
year: 1969,
color: 'red'
};
const entries = Object.entries(car);
for (const [key, value] of entries) {
console.log(`${key}: ${value}`);
}
// Output:
// brand: Ford
// model: Mustang
// year: 1969
// color: red
This page was updated on -
Found an error or have feedback on our docs?
Create an issue on GitHub and let us know! Your input helps improve our documentation for everyone in the community.
Report error, send feedback on Github