How to convert an object to an array in JavaScript?

To convert an object to an array in JavaScript, use the Object.entries function.

Object.entries converts an object to an array of 2-element arrays.

The first element is the key and the second one is the value.

Here's how you do it:

const object = {
  someKey: 'Hello World!',
  anotherKey: 123
};

const array = Object.entries(object);

console.log(array); // [ [ 'someKey', 'Hello World!' ], [ 'anotherKey', 123 ] ]