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 Object.keys() method to get the keys and then use map() method to map the keys to the values and store the values in an array.
-
Example: This example implements the above approach.
<!DOCTYPE HTML><html><head><title>Alternative version for Object.values() in JavaScript.</title></head><bodystyle="text-align:center;"><h1style="color: green">GeeksForGeeks</h1><pid="GFG_UP"></p><buttononclick="gfg_Run()">Click Here</button><pid="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></html>chevron_rightfilter_none -
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.
<!DOCTYPE HTML><html><head><title>Alternative version for Object.values() in JavaScript.</title></head><bodystyle="text-align:center;"><h1style="color: green">GeeksForGeeks</h1><pid="GFG_UP"></p><buttononclick="gfg_Run()">Click Here</button><pid="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></html>chevron_rightfilter_none -
Output:
Approach 3:(In ES6 format) Use Object.keys() method to get the keys and then use map() method to map the keys to the values and store the values in an array.
-
Example: This example implements the above approach.
<!DOCTYPE HTML><html><head><title>Alternative version for Object.values() in JavaScript.</title></head><bodystyle="text-align:center;"><h1style="color: green">GeeksForGeeks</h1><pid="GFG_UP"></p><buttononclick="gfg_Run()">Click Here</button><pid="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></html>chevron_rightfilter_none -
Output:
Recommended Posts:
- Implement prepend and append with regular JavaScript
- Implement a JavaScript when an element loses focus
- How to implement a filter() for Objects in JavaScript?
- JQuery | How to implement Star-Rating system using RateYo
- Design and Implement Calculator using jQuery
- How to implement Facebook login in your Web app with Firebase ?
- How to implement Google Login in your Web app with Firebase ?
- How to implement 'add tag' functionality in an Angular 9 app ?
- Ways of iterating over a array in JavaScript.
- Creating objects in JavaScript (4 Different Ways)
- How to declare variables in different ways in JavaScript?
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


