Categories
JavaScript Answers

How to convert a JavaScript object to array?

Spread the love

Sometimes, we want to convert a JavaScript object to array.

In this article, we’ll look at how to convert a JavaScript object to array.

How to convert a JavaScript object to array?

To convert a JavaScript object to array, we can use the Object.values method to return the object property values in an array.

For instance, we write:

const o = {
  0: "a",
  1: 'b'
};
const a = Object.values(o);
console.log(a)

To call Object.values with o.

Then we see that a is ['a', 'b'] from the console log.

Conclusion

To convert a JavaScript object to array, we can use the Object.values method to return the object property values in an array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *