All TalkersCode Topics

Follow TalkersCode On Social Media

Convert Object To Array JavaScript

Last Updated : Jan 1, 2023

Convert Object To Array JavaScript

In this tutorial we will show you the solution of convert object to array JavaScript, JavaScript is not an object oriented language, but it has many ways with help of which you can use objects in JavaScript.

Let us understand our today’s article now.

Step By Step Guide On Convert Object To Array JavaScript :-

As, there are many ways to convert object to array like object.keys(), object.values() and object.entries().

Here, there are different ways but the method or say pattern remain same.

You can use any from them for conversion. Let us see the codes for better understanding.

The code is as:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>convert object to array using JavaScript</title>
</head>
<body>
<script>
  const values = {
       firstName: 'Robert',
       lastName: 'Downey'
  };
const resultName = Object.keys(values);
console.log(resultName);
const resultValues = Object.values(values);
console.log(resultValues);
const resultEntries = Object.entries(values);
console.log(resultEntries);
</script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Hence, here inside body we directly use our script tag to write code of JavaScript in html file.
  6. Now, inside JavaScript code. Here we create an object named as values and assigned some values in key and values patterns.
  7. After this, we again create three constants resultName, resultValues and resultEntries. And here first one is created to show keys with help of arrays, the second one is used for values of keys and last is used to show full entries here.
  8. Inside all, we write their respective codes like object.keys for keys, object.values for values and object.entries for entries. We use them of our values and use in resultName, resultValues and resultEntries respectively.
  9. Now, at last to see the outputs we directly print them in console usin comsole.log() can be seened with help of inspecting our source page. Then go to console tab to see the output. We hope that you understand this codes properly.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to convert an object to array using JavaScript.

I hope this tutorial on convert object to array JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials