Object.keys() Object.values() & Object.entries() in JavaScript

JavaScript

In this tutorial we will explain Object.keys() Object.values() and Object.entries() in JavaScript. In previous article we have explained how to create an Object In JavaScript.

  • Object.keys() is the feature of the ES6 and returns an array of keys.
  • Object.values() is the feature of the ES8 and returns an array of values.
  • Object.entries() is the feature of the ES8 and returns an array of keys/values pair.

Object.keys()

Object.keys() is the feature of the ES6. Using this method you can find the keys of an object in the form of an array. Object.keys() returns an array of keys.

let obj = {
    fname: 'web',
    lname: 'study'
};
console.log(Object.keys(obj)) // ["fname", "lname"]

======================================================================

let array = ['Web', 'study']
console.log(Object.keys(array)); // ["0", "1"]

======================================================================

let obj = {
    2: 'Web',
    1: 'Study'
}
console.log(Object.keys(obj)); // ["1", "2"]

In the above example you have seen, we have used Object.keys() for object and array. When we used this method for an object then it returns keys of an object in the form of an array while in case of array it returns index position of an array.

If we have numeric keys of an object in random order then Object.keys() returns an array of keys in the ascending order.

Object.values()

Object.values() is the feature of the ES8. Using this method you can find the values of an object in the form of an array. Object.values() returns an array of values.

var obj = {
    fname: 'web',
    lname: 'study'
};

console.log(Object.values(obj)) // ["web", "study"]

========================================================================

let array = ['Web', 'study']
console.log(Object.values(array)); // ["Web", "study"]

=========================================================================

let obj = {
    2: 'Web',
    1: 'Study'
}
console.log(Object.values(obj)); // ["study", "web"]

In this example you have seen, we have used Object.values() for object and array. When we used this method for an object then it returns values of an object in the form of an array while in case of array it returns also values of an array.

If we have numeric keys of an object in random order then Object.values() returns an array of values according to their keys in ascending order.

Object.entries()

Object.entries() is the feature of the ES8. Using this method you can find the key/values of an object in the form of an array. Object.entries() returns an array of key/values pair.

var obj = {
    fname: 'web',
    lname: 'study'
};

console.log(Object.entries(obj)) // [["fname", "web"], ["lname", "study"]]

Find the length of an Object

If you want to find the length of an Object then you can use Objectkeys() method.

var obj = {
    fname: 'web',
    lname: 'study'
};

console.log(Object.keys(obj).length) // 2

So in this article you have seen, Object.keys() returns an array of keys and Object.values() returns an array of values and Object.entries() returns an array of key/value pair.

So using this tutorial anyone can easily understand Object.keys() Object.values() and Object.entries() in JavaScript.

If you want to add something or improve or you find anything wrong/incorrect in this article then please leave a message using below comment box.

Leave a Reply

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

20 − fifteen =