How to Get Keys from an Object in JavaScript

How to get keys from an object in javascript
How to get keys from an object in javascript

Getting all the keys from an object in javascript

Object.keys method is used to get all the keys from an object. You just have to pass the object as a parameter to get the keys. Here is an example.

The output of the above code will be

Array [“name”, “age”, “profession”, “isActive”]

Using Object.keys() method with an array

You can use this method with an array to get all the indexes from an array. It just asks you to pass an array as a parameter instead of an object. Here is an example.

The output of the above code will be

Array [“0”, “1”, “2”]

What is the return type of Object.keys()?

An array of strings is the return type of object.keys method. This array of strings have all the keys of an object or array which was passed as a parameter.

For more details please check developer.mozilla.org

Conclusion

Object.keys method is a built-in javascript function that can extract all the keys from an object or array. It returns a new array with all the keys of an object or array. It expects a parameter that can be an array or object.

LEAVE A REPLY

Please enter your comment!
Please enter your name here