DEV Community

loading...

Javascript Object.keys( ) method

victoromondi1997 profile image VICTOR OMONDI ・1 min read

Want to get all keys present in an object?

Try Object.keys( )

Usecase:

let obj = {
    year: 2019,
    month: 'september'
}

const objKeys = Object.keys(obj);            

console.log(objKeys);

// [ 'year', 'month' ]

Discussion (2)

pic
Editor guide
Collapse
tabithakavyu profile image
Thabitha Kavyu • Edited
let users = {
  Victor: {
    age: 27,
    online: false
  },
  Zuzou: {
    age: 32,
    online: true
  },
  Aliyu: {
    age: 48,
    online: false
  },
  Sodiq: {
    age: 19,
    online: true
  }
};

function getArrayOfUsers(obj) {

   return Object.keys(obj);

}

console.log(getArrayOfUsers(users));```


Collapse
victoromondi1997 profile image
VICTOR OMONDI Author • Edited
['Victor', 'Zuzou', 'Aliyu', 'Sodiq']