How to get all property values of a JavaScript Object (without knowing the keys) ? - GeeksforGeeks

28 Nov, 2019
Method 1: Using Object.values() Method: The Object.values() method is used to return an array of the object’s own enumerable property values. The array can be looped using a for-loop to get all the values of the object. Therefore, the keys are not required to be known to get all the property values.
Syntax:
let valuesArray = Object.values(exampleObj);  for (let value of valuesArray) {    console.log(value);}
Example:
<!DOCTYPE html><html><head>  <title>    How to get all properties    values of a Javascript Object    (without knowing the keys)?  </title></head><body>  <h1 style="color: green">    GeeksforGeeks  </h1>  <b>    How to get all properties    values of a Javascript Object    (without knowing the keys)?  </b>  <p>    Click on the button to get all    properties values.  </p>  <p>    Check the console for the output  </p>  <button onclick="getValues()">    Get Property Values  </button>  <script type="text/javascript">    function getValues() {      let exampleObj = {        language: "C++",        designedby: "Bjarne Stroustrup",        year: "1979"      };        let valuesArray = Object.values(exampleObj);        for (let value of valuesArray) {        console.log(value);      }    }  </script></body></html>
Output:
Before clicking the button:
After clicking the button:
Method 2: Extracting the keys to access the properties: The Object.keys() method is used to return an array of objects own enumerable property names. The forEach() method is used on this array to access each of the keys. The value of each property can be accessed using the keys with an array notation of the object.Therefore, the keys are not required to be known beforehand to get all the property values.
Syntax:
let objKeys = Object.keys(exampleObj);  objKeys.forEach(key => {    let value = exampleObj[key];      console.log(value);});
Example:
<!DOCTYPE html><html><head>   <title>    How to get all properties    values of a Javascript Object    (without knowing the keys)?  </title></head><body>  <h1 style="color: green">    GeeksforGeeks  </h1>  <b>    How to get all properties    values of a Javascript Object    (without knowing the keys)?  </b>  <p>    Click on the button to get all    properties values.  </p>  <p>    Check the console for the output  </p>  <button onclick="getValues()">    Get Property Values  </button>  <script type="text/javascript">    function getValues() {      let exampleObj = {          language: "C++",          designedby: "Bjarne Stroustrup",          year: "1979"        };        let objKeys = Object.keys(exampleObj);        objKeys.forEach(key => {        let value = exampleObj[key];          console.log(value);      });    }  </script></body></html>
Output:
Before clicking the button:
After clicking the button:
JavaScript-Misc
Picked
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript