Open In App

JavaScript Object values() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript object.values() method is used to return an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by the object manually if a loop is applied to the properties. 

Object.values() takes the object as an argument of which the enumerable property values are to be returned and returns an array containing all the enumerable property values of the given object.

Syntax:

Object.values(obj);

Parameters:

  • obj: It is the object whose enumerable property values are to be returned.

Return Value:

  • Returns an array containing all the enumerable property values of the given object. 

Example 1: In this example, an array “check” has three property values [‘x’, ‘y’, ‘z’] and the object.values() method returns the enumerable property values of this array. The ordering of the properties is the same as that given by the object manually. 

javascript
// Returning enumerable property values of a simple array 
let check = ['x', 'y', 'z'];
console.log(Object.values(check));

Output: 

Array ["x", "y", "z"]

Example 2: In this example, an array-like object “check” has three property values { 0: ’23’, 1: ‘geeksforgeeks’, 2: ‘true’ } and the object.values() method returns the enumerable property values of this array. The ordering of the properties is the same as that given by the object manually. 

javascript
// Returning enumerable property values
// of an array like object. 
let object = { 0: '23', 1: 'geeksforgeeks', 2: 'true' };
console.log(Object.values(object))

Output:  

Array ["23", "geeksforgeeks", "true"]

Example 3: In this example, an array-like object “check” has three property values { 70: ‘x’, 21: ‘y’, 35: ‘z’ } in random ordering and the object.values() method returns the enumerable property values of this array in the ascending order of the value of indices.

javascript
// Returning enumerable property values
// of an array like object. 
let object = { 70: 'x', 21: 'y', 35: 'z' };
console.log(Object.values(object));

Output:  

 Array ["y", "z", "x"]

Applications:

  • Object.values() is used for returning enumerable property values of a simple array, array-like object, and array-like object with random key ordering.

Exceptions:

  • It causes a TypeError if the argument passed is not an object.
  • If an object is not passed as an argument to the method, then it persuades it and treats it as an object.

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • safari

JavaScript Object values() Method- FAQs

What is the values() method in JavaScript?

The values() method in JavaScript is used to return an array of a given object’s own enumerable property values. It provides an easy way to extract all values from an object into an array.

Does the values() method include inherited properties?

No, the values() method does not include inherited properties. It only includes the object’s own enumerable properties.

What happens if the values() method is used on a non-object argument?

If you use the values() method on a non-object argument, JavaScript will attempt to convert the argument to an object.

Can Object.values() be used on arrays?

Yes, Object.values() can be used on arrays, and it will return an array of the array’s values.

What are some common use cases for values()?

Some common use cases include:

  • Extracting values from an object for iteration or manipulation.
  • Converting an object into an array of its values for easier processing.
  • Using object values in functional programming paradigms such as map, filter, and reduce.

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: