88.8k views
1 vote
How to turn object into array javascript

User Jaik Dean
by
8.3k points

1 Answer

3 votes

Final answer:

To turn an object into an array in JavaScript, use Object.keys() for an array of keys, Object.values() for an array of values, and Object.entries() for key-value pairs in the form of arrays.

Step-by-step explanation:

To turn an object into an array in JavaScript, you may want to transform either the object's properties or values into an array. Take for example the following object:

{
key1: 'value1',
key2: 'value2',
key3: 'value3'
}

You can use Object.keys() to create an array of keys or Object.values() to create an array of values. To get both keys and values as an array, you can use Object.entries().

Example:

const obj = {key1: 'value1', key2: 'value2', key3: 'value3'};

// To get an array of keys
const keys = Object.keys(obj);
// keys is ['key1', 'key2', 'key3']

// To get an array of values
const values = Object.values(obj);
// values is ['value1', 'value2', 'value3']

// To get an array of entries (key-value pairs)
const entries = Object.entries(obj);
// entries is [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']]

The complete question is: How to turn object into array javascript is:

User IndigoFire
by
7.3k points