What is Object.values in JavaScript?
By using Object.values, we can get the values of the object. This method will return an array that contains the object’s own enumerable property values. The order of the returned values is the same as the order that it occurs in the for…in loop.
Syntax
Object.values(obj)
Example
let employee = {companyName : "Educative"};let manager = Object.create(employee);manager.name = "Ram";manager.designation = "Manager";manager.getName = function(){return this.name;}manager[Symbol("symbol")] = "symbol";Object.defineProperty(manager, "non-enumerable", {enumerable: false})console.log("The manager object is ", manager);console.log("\nValues of the manager object is ", Object.values(manager))// the values of non-enumerable properties,symbols and inherited properties are not included in the keys
In the above code, we have:
- Created the
managerobject from theemployeeobject. - Added
symbolas a property key to themanagerobject. - Added a
non-enumerableproperty to themanagerobject.
When we call the Object.values method by passing the manager object as an argument, we will get an array of its own object property values. The returned array doesn’t include the values of:
- Symbols
Inherited properties In our case, employeeobject properties are inherited properties, so those property values are not included.Non-enumerable properties The properties that don’t occur in the for…in loop; in our case, the non-enumerableproperty ofmanagerobject.