Let's consider an object called John
.
const John = {
name: "John Doe",
age: 32,
bio: "John Doe is a great guy",
};
We can get all the properties in this object using the Object.getOwnPropertyNames()
method.
const John = {
name: "John Doe",
age: 32,
bio: "John Doe is a great guy",
};
// get all property names
const arr = Object.getOwnPropertyNames(John);
/*
Result:
-------
["name", "age", "bio"]
*/
- The
Object.getOwnPropertyNames()
method accepts the object to get all the property names as the parameter. - It returns an array containing the property names.