ES8 - Object entries

This tutorial covers Object entries introduced to latest javascript features introduced in ES8, ES2017..

Object Enumerable and non enumerable properties

Every object has key and values enclosed in parenthesis.

Object example

//inline object creation and initilization
const myobject = {

myobject.class="5th class"; // assignment syntax

The above example created an object with several properties - studentid,studentname and class An object has enumerable and non enumerable properties of an object.

for(let key in myobject){
  console.log(key +'-'+myobject[key])

Output is

class-5th class

As you seen the above example, During iteration of object, properties are printed to the console. These properties are called enumerable properties.

We have created object wit inline syntax and assigned data using assignment syntax. We can add properties to an object using Object.defineProperty and called object non enumerable properties.These are displayed during iteration of object. enumerable:false makes property as non enumerable.


ES8 introduced following two new methods to Object class

  • Object.entries()
  • Object.values()

This post talks about object.entries() method and its examples.

object entries method

How do you iterate object? So for-in loop used to iterate each element of an object.

Object.entries() method returns array of elements with each element is in the format of [key,value] in the same order.

So what is special about object.entries() method, as for-in loop also do the same thing.

entries method returns own enumerable properties not the properties inherited from prototype inheritance. for-in loop returns enumerable properties of an object and its parent via prototype inheritance.



Parameters and returns

  • Input parameters - myobject is input provided to iterate the elements
  • Return type - returns array of object key and values.

Let’s see an example to print key and properties in array

Output is 
[ [ 'studentid', '1' ],
  [ 'studentname', 'Tom' ],
  [ 'class', '5th class' ] ]

Support This method supports latest browsers, old browsers need not have a support for it.

Polyfill is required to support in old browsers.