Get an Object's Values as Array in JavaScript


Borislav Hadzhiev

Sun Oct 10 20211 min read


Photo by Aubrey Odom

Get an Object's Values as Array in JavaScript #

To get the values of an object as an array, use the Object.values() method, passing it the object as a parameter. The method returns an array containing the object's property values in the same order as provided by a for ... in loop.

const obj = {id: 1, country: 'Chile', city: 'Santiago'}; const values = Object.values(obj); // ๐Ÿ‘‡๏ธ [1, 'Chile', 'Santiago'] console.log(values);

In the code snippet we use the Object.values method to get the object's values.

The Object.values method is not supported in Internet Explorer. If you have to support the browser, use the next approach this article covers.
// Supported in IE 9-11 const obj = {id: 1, country: 'Chile', city: 'Santiago'}; // ๐Ÿ‘‡๏ธ ['id', 'country', 'city'] const keys = Object.keys(obj); console.log(keys); const values = => { return obj[key]; }); // ๐Ÿ‘‡๏ธ [1, 'Chile', 'Santiago'] console.log(values);

We use the Object.keys method to get an array containing the object's keys.

We then iterate over the keys using and return the value for each key.

The function we pass to the map method gets invoked for each key in the keys array.

The map method returns a new array containing the values we returned from the callback function passed to the method.

This approach is a bit more indirect and verbose, however if you have to support Internet Explorer, it gets the job done.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee