Summary: in this tutorial, you will learn how to use the JavaScript Object.entries() method.
Introduction to JavaScript Object.entries() method #
ES2017 introduces the Object.entries() method that accepts an object and returns its own enumerable string-keyed property [key, value] pairs of the object.
Here is the syntax of the Object.entries() method:
Object.entries()See the following example:
const ssn = Symbol('ssn');
const person = {
firstName: 'John',
lastName: 'Doe',
age: 25,
[ssn]: '123-345-789'
};
const kv = Object.entries(person);
console.log(kv);Output:
[
['firstName', 'John'],
['lastName', 'Doe'],
['age', 25]
]In this example:
- The
firstName,lastName, andageare own enumerable string-keyed property of thepersonobject, therefore, they are included in the result. - The
ssnis not a string-key property of the person object, so it is not included in the result.
Object.entries() vs. for…in #
The main difference between the Object.entries() and the for...in loop is that the for...in loop also enumerates object properties in the prototype chain.
Thank you for your feedback!