Categories
JavaScript Answers

How to convert JavaScript object with numeric keys into array?

Spread the love

Sometimes, we want to convert JavaScript object with numeric keys into array.

In this article, we’ll look at how to convert JavaScript object with numeric keys into array.

How to convert JavaScript object with numeric keys into array?

To convert JavaScript object with numeric keys into array, we can use the array map method with Object.keys.

For instance, we write

const arr = Object.keys(obj).map((k) => {
  return obj[k];
});

to call Object.keys with obj to return the an array of keys in obj.

Then we call map with a callback to return the value of property k in obj.

Conclusion

To convert JavaScript object with numeric keys into array, we can use the array map method with Object.keys.

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 *