Skip to main content
javascript-object-keys-example

JavaScript Object Keys Example | Object.keys()

This is a quick JavaScript tutorial that helps to understand the usage of Object.keys() method with example. This is a very popular JavaScript method for objects. You can get any object keys using object.keys() method.

The object values can be retrieved using object.values() method. The return keys/values ordering is the same as the provided object.

Object.Keys() method Uses & Example

The JavaScript Object.keys() method returns an array of the given object’s property names. The keys may be array index or object named keys. The object.keys return the array of strings that have enumerable properties of the passed object.

The syntax for Object.keys() method is following.-

Object.keys(obj)

The obj is the source object and required parameter.

How To Get JavaScript Object Keys

We will create an employee object and will get keys.The return value is an array of enumerable properties of a given object.

// app.js

let emp_details = {
    emp_name: 'Adam',
    emp_salary: 12344,
	emp_age : 25
} ;
console.log(Object.keys(emp_details));

Now open the command line or browser and run the above code, We will get an array of object keys. The result would be as follows –

['emp_name', 'emp_salary', 'emp_age']

We can also get the array keys using Object.keys() method. Let’s take a simple example –

let status_arr = ['pending', 'compeleted', 'in-progress', 'falied'];
console.log(Object.keys(status_arr));

The above code will give following results –

['0', '1', '2', '3']

How to Get The Length of an Object in JavaScript

We can also get the object length using the JavaScript object.length property, sometimes we need a length of the object for operation.That also helps to know – How many keys have a JavaScript object.

// app.js

let emp_details = {
    emp_name: 'Adam',
    emp_salary: 12344,
	emp_age : 25
} ;
console.log(Object.keys(emp_details).length);

The Output –
3

How to Iterate on Object.keys()

We can get each element of response of Object.keys() method using Array.forEach().The Array.forEach() method help to iterate through all the keys.

// app.js

let emp_details = {
    emp_name: 'Adam',
    emp_salary: 12344,
	emp_age : 25
} ;
Object.keys(emp_details).forEach(item => {
  console.log(item);
});

The Output of Object.keys() using Array.forEach()

emp_name,
emp_salary,
emp_age

How to Sort Object keys

The javascript ES6 has Array.Sort() method to sort the array elements.This method takes the source array as an argument.

let emp_details = {
    emp_name: 'Adam',
    emp_salary: 12344,
	emp_age : 25
} ;
console.log(Object.keys(emp_details).sort());

The Output of Object.keys() using Array.forEach() –

[ 'emp_age', 'emp_name', 'emp_salary' ]

How to Find Element into Object keys

The javascript ES6 has Array.find() method to an element into the array.I am finding emp_name key is an exit or not into the object keys.

let emp_details = {
    emp_name: 'Adam',
    emp_salary: 12344,
	emp_age : 25
} ;
let keys = Object.keys(emp_details);
console.log(keys);
let find_key = keys.find(k => k=='emp_name')
console.log(find_key);

The Output of Array.find()

[ 'emp_name', 'emp_salary', 'emp_age' ]
 emp_name

I have completed a tutorial on JavaScript Object.keys() with an example.

Leave a Reply

Your email address will not be published. Required fields are marked *