FrontendDeveloper.in

ECMAScript question detail

Object entries

The Object.entries() method is introduced to returns an array of a given object's own enumerable string-keyed property [key, value] pairs in the same order as for...in loop.

const countries = {
IN: 'India',
SG: 'Singapore',
}
Object.entries(countries) // [["IN", "India"], ["SG", "Singapore"]]

By the way, non-object argument will be coerced to an object

const countriesArr = ['India', 'Singapore'];
console.log(Object.entries(countriesArr)); // [ ['0', 'India'], ['1', 'Singapore']]

const country = 'India';
console.log(Object.entries(country)); // [["0", "I"], ["1", "n"], ["2", "d"], ["3", "i"], ["4", "a"]]

console.log(Object.entries(100)); // [], an empty array for any primitive type because it won't have any own properties
Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399