Hello Friends đź‘‹,

Welcome to Infinitbility.

Sometime we need get all keys of object, like validating objects details, manage object as daynamics keys and all values.

In this times, we need all keys list whitch we will use to get objects values.

Mostly, we need to maintain daynamic object whitch we don’t how much keys we get.

Now, let’s move to solution, how we get all keys and how to use them for get values from object in javascript.

let assume, we have below example object whitch we will use to get all keys example and use keys example.

let User = {
  id: 1,
  name: "infinitbility",
  slogan: "We have the ability to build infinite way for us.",
  dob: "2021-07-26",
  isActive: 1
};

Javascript, Get all keys

Javascript provide Object.keys() method to get all keys in array, Yes it will return array of keys.

let User = {
  id: 1,
  name: "infinitbility",
  slogan: "We have the ability to build infinite way for us.",
  dob: "2021-07-26",
  isActive: 1
};

let userKeys = Object.keys(User);
console.log("userKeys =>", userKeys);

Output

Javascript Get All keys Example
Javascript Get All keys Example

Javascript, Use retrived keys example

To use keys get using Object.keys() method, we will use loop of keys and pass each keys in object to get value.

let User = {
  id: 1,
  name: "infinitbility",
  slogan: "We have the ability to build infinite way for us.",
  dob: "2021-07-26",
  isActive: 1
};

let userKeys = Object.keys(User);
console.log("userKeys =>", userKeys);

for(let key of userKeys){
  console.log(key, " => ", User[key]);
}

Output

Javascript Use keys Example
Javascript Use keys Example

Thanks for reading…