Example(1)
<h2 id="demo1"></h2> <script> var person = {name:"Pinky", age:30, city:"New York"}; document.getElementById("demo1").innerHTML = person; </script>
Using Object.values()
Any JavaScript object can be converted to an array using Object.values()
:
var person = {name:"John", age:30, city:"New York"}; var myArray = Object.values(person);
myArray
is now a JavaScript array, ready to be displayed:
Example(2)
<script> var person = {name:"Roopa", age:50, city:"New York"}; var myArray = Object.values(person); document.getElementById("demo1").innerHTML = myArray; </script>
Object.values()
is supported in all major browsers since 2016.
Complete Code For Displaying Objects Using Object Value Method In JS
<!DOCTYPE html> <html> <head> <title>How To Display Objects Using Object Value Method In JS</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> </head> <style> body{ background: black; } </style> <body> <div class="container"> <br> <div class="text-center"> <h1 id="color" style="color: White">Display Objects Using Object Value Method In JS</h1> </div> <br> <br> <div class="well"> <h2 id="demo1"></h2> <script> var person = {name:"Roopa", age:50, city:"New York"}; var myArray = Object.values(person); document.getElementById("demo1").innerHTML = myArray; </script> </div> </div> </body> </html>