How to get key of object in javascript?
1 answer(s)
This is pretty simple in JavaScript. The easiest way is Object.keys()
. You just give it your object, like this: Object.keys(myObject)
. It will give you an array back with all the keys as strings. So if your object is { name: 'Kylie', age: 25 }
, you'll get ['name', 'age']
. It's really handy.
You can also use a for...in
loop if you want to do something with each key one by one. Like for (const key in myObject)
. This lets you go through them all. Give it a go, no worries.