Object.values()

Why Object.values ?

The Object.values () method returns the enumerable property values entered into the object as an array. The order in the array is the same as the properties in the object, respectively.

In other words, Object.values () takes the object as an argument to return its enumerable property values and returns an array containing all the enumerable property values of the given object.

The Object.values () method does the opposite of what the Object.getOwnPropertyNames () method does. The Object.getOwnPropertyNames () method returns an array of properties within the object. The Object.values () method does not return properties as an array, but returns an array of values against it.

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.

Syntax:

Object.values( myObj )

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

Return Value:

  • An array containing the given object’s own enumerable property values.

JavaScript Object.values Examples

Example 1

  • In the following example, I first defined an array (myObj). Then I entered four properties into this series.
  • I stepped outside the object and applied Object.values ​​(). I assigned this to arr. Then I returned this variable with the help of consol.log.
        var myObj = {
            prop1: "first",
            prop2: "second",
            prop3: "third",
            prop4: "forth"
        };

        let arr = Object.values(myObj);

        console.log(arr);

output:

[“first”, “second”, “third”, “forth”]


Example 2

The Object.values () method does the opposite of what the Object.getOwnPropertyNames () method does. The Object.getOwnPropertyNames () method returns an array of properties within the object. The Object.values () method does not return properties as an array, but returns an array of values against it.

        var people = {
            person1: "alex",
            person2: "maria",
            person3: "rick",
            person4: "john"
        };

        let valuesObj = Object.values(people);
        let propertyNames = Object.getOwnPropertyNames(people);

        console.log(valuesObj);
        console.log(propertyNames);

output:

[“alex”, “maria”, “rick”, “john”]
[“person1”, “person2”, “person3”, “person4”]


Example 3

        var names = {
            name1: "sasha",
            name2: "oliver",
            name3: "barry",
            name4: "bob"
        };

        let myObj = Object.create(names);

        let myObjValues = Object.values(names);

        console.log(myObjValues);

output:

[“sasha”, “oliver”, “barry”, “bob”]


Sources:


Browser Support

Chrome54
Edge14
Firefox47
Internet Explorerno
Opera41
Safari10.1
Android webview54
Chrome for Android54
Edge mobileyes
Firefox for Android47
Opera Android41

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started