Thursday, September 4, 2025
HomeLanguagesJavascriptWays to implement Object.values() in JavaScript

Ways to implement Object.values() in JavaScript

There is a method Object.values() which returns the values of JavaScript Object. Here we are going to see all other alternatives to this method with the help of JavaScript. Here a few approaches are discussed. 

Approach 1: Use the Object.keys() method to get the keys and then use the map() method to map the keys to the values and store the values in an array.

Example: This example implements the above approach. 

html




<body style="text-align:center;">
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
    <h3>
        Alternative version for Object.values() in JavaScript.
    </h3>
    <p id="GFG_UP">
    </p>
    <button onclick="gfg_Run()">
        Click Here
    </button>
    <p id="GFG_DOWN">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var arr = [];
        var obj = {
            a: 'val_1',
            b: 'val_2'
        };
        el_up.innerHTML =
        "Click on the button to get the values.<br>Object = "
        + JSON.stringify(obj);
          
        function gfg_Run() {
            var val = Object.keys(obj).map(function(e) {
                return obj[e];
            });
            el_down.innerHTML = JSON.stringify(val);
        }
    </script>
</body>


Output:

 

Approach 2: Visit every property of the object by running a loop and push the value in an array each time. obj.hasOwnProperty() and push() method are used.

Example: This example implements the above approach. 

html




<body style="text-align:center;">
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
    <h3>
        Alternative version for Object.values() in JavaScript.
    </h3>
    <p id="GFG_UP">
    </p>
    <button onclick="gfg_Run()">
        Click Here
    </button>
    <p id="GFG_DOWN">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var arr = [];
        var obj = {
            a: 'val_1',
            b: 'val_2'
        };
        el_up.innerHTML =
        "Click on the button to get the values.<br>Object = "
        + JSON.stringify(obj);
          
        function getValues(obj) {
            var ret = [];
            for (var i in obj) {
                if (obj.hasOwnProperty(i)) {
                    ret.push(obj[i]);
                }
            }
            return ret;
        }
          
        function gfg_Run() {
            el_down.innerHTML = JSON.stringify(getValues(obj));
        }
    </script>
</body>


Output: 

 

Approach 3:(In ES6 format) Use the Object.keys() method to get the keys and then use the map() method to map the keys to the values and store the values in an array.

Example: This example implements the above approach. 

html




<body style="text-align:center;">
    <h1 style="color: green">
        GeeksForGeeks
    </h1>
    <h3>
        Alternative version for Object.values() in JavaScript.
    </h3>
    <p id="GFG_UP">
    </p>
    <button onclick="gfg_Run()">
        Click Here
    </button>
    <p id="GFG_DOWN">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var arr = [];
        var obj = {
            a: 'val_1',
            b: 'val_2'
        };
        el_up.innerHTML =
        "Click on the button to get the values.<br>Object = "
        + JSON.stringify(obj);
          
        function gfg_Run() {
            el_down.innerHTML = Object.keys(obj).map(e => obj[e]);
        }
    </script>
</body>


Output: 

 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS