How to use Object.keys() In Javascript
By using the Object.keys(obj) method, we will get the array of keys, which we will further use for extracting the data. We have defined a function that retrieves the values by using an index using the method Object.keys().
Example: Let’s see an example to demonstrate the above mentioned approach.
Javascript
let obj = { a: "abc" , b: "cde" , findByIndex: function (n) { return this [Object.keys( this )[n]]; } }; console.log(obj.findByIndex(1)); |
Output
cde
JavaScript Program to Access Non-Numeric Object Properties by Index
In JavaScript, we can access non-numeric object properties by their keys (property names) rather than by index, as objects are not indexed like arrays. Objects use key-value pairs to store data. But there are certain ways to achieve this using the properties of the objects.