Get All Keys of a Javascript Object

While working with javascript objects, we often need to get all the keys to process them in some way. This article demonstrates easy ways to get and traverse all the keys of a javascript object using different methods and various example illustrations.

Table of Contents:

Get All Keys of a Javascript Object using Object.keys()

Javascript’s Object.keys() returns an array of enumerable property names of the object. The method iterates the keys in the same order as that of a standard loop.

Example:-

Get all the keys of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’}

Advertisements

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};
var keys = Object.keys(personObject);
console.log('All the keys of the object are:  ' +  keys);

Output:-

All the keys of the object are:  personFirstName,personLastName,dateOfBirth,city

Get All Keys of a Javascript Object using iteration

Here, in the below code, we are using the iteration as for many browsers Object.keys() will not work.

Example:-

Get all the keys of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’}

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};
var keys = [];
for (var k in personObject) 
{
  // using push() to add the keys to the array
  keys.push(k);
}
console.log('All the keys of the object are:  ' +  keys);

Output:-

All the keys of the object are:  personFirstName,personLastName,dateOfBirth,city

Get All Keys for a Javascript Object and not its Derived ProtoType Keys

Javascript’s hasOwnProperty() method determines if the particular property is its own property or not (as opposed to inheriting it). The returned value is boolean.

Example:-

Get all the keys of the object { personFirstName: ‘George’, personLastName: ‘Smith’, dateOfBirth: ‘Nov 14 1984’ , city : ‘Santiago’}

Code:-

let personObject =  { personFirstName: 'George', personLastName: 'Smith', dateOfBirth: 'Nov 14 1984' , city : 'Santiago'};  
var keys = [];
    for (var k in personObject) 
    {
      // check if the property is its own and not inherited
        if (!personObject.hasOwnProperty(k)) 
        // if inherited then do not add to keys array and continue to loop
            continue;
            keys.push(k);
    }
    console.log('All the keys of the object are:  ' +  keys);

Output:-

All the keys of the object are:  personFirstName,personLastName,dateOfBirth,city

Explanation:-

Here, in the above code, we are looping the object personObject and adding the keys to an array(variable keys) only if the property is of the original object and is not one of the inherited properties. Therefore a check is done within the for loop using the hasOwnProperty() method.

I hope this article helped you to get all the keys of an object in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top