Show
Convert JavaScript Map to Array of Objects
Let's suppose you have the following JavaScript const myMap = new Map(); myMap.set('foo', 1); myMap.set('bar', 2); myMap.set('baz', 3); To convert it into an array of objects, for example, like so: /* [ {key: 'foo', value: 1}, {key: 'bar': value: 2}, {key: 'baz': value: 3}, ] */ You can do any of the following:
Using Array.from() Since const arr = Array.from(myMap); console.log(arr); // [['foo', 1], ['bar', 2], ['baz', 3]] To transform this array of arrays into an array of objects, you can simply pass a map function as the second (optional) argument to const arr = Array.from(myMap, function (item) { return { key: item[0], value: item[1] } }); console.log(arr); /* [ {key: 'foo', value: 1}, {key: 'bar': value: 2}, {key: 'baz': value: 3}, ] */ You can shorten the map function to a one-liner by using; an arrow function, and destructing syntax in the callback function argument, like so: const arr = Array.from(myMap, ([key, value]) => ({ key, value })); console.log(arr); /* [ {key: 'foo', value: 1}, {key: 'bar': value: 2}, {key: 'baz': value: 3}, ] */ Using the Spread Operator With Array.prototype.map() You can use the spread operator on const arr = [...myMap]; console.log(arr); // [['foo', 1], ['bar', 2], ['baz', 3]] As you can see, this results in an array of arrays containing key and value from the const arr = [...myMap].map(function (item) { return { key: item[0], value: item[1] } }); console.log(arr); /* [ {key: 'foo', value: 1}, {key: 'bar': value: 2}, {key: 'baz': value: 3}, ] */ You can shorten the map function to a one-liner by using; an arrow function, and destructing syntax in the callback function argument, like so: const arr = [...myMap].map(([key, value]) => ({ key, value })); console.log(arr); /* [ {key: 'foo', value: 1}, {key: 'bar': value: 2}, {key: 'baz': value: 3}, ] */ Iterating Over Map and Adding Elements to Array You can simply iterate over the For example, you can use const arr = []; myMap.forEach((value, key) => arr.push({ key, value })); console.log(arr); /* [ {key: 'foo', value: 1}, {key: 'bar': value: 2}, {key: 'baz': value: 3}, ] */ Or, alternatively, you may use the const arr = []; for (const [key, value] of myMap) { arr.push({ key, value }); } console.log(arr); /* [ {key: 'foo', value: 1}, {key: 'bar': value: 2}, {key: 'baz': value: 3}, ] */ Hope you found this post useful. It was published 29 Sep, 2021. Please show your love and support by sharing this post.
ExamplesReturn a new array with the square root of all element values: const numbers = [4, 9, 16, 25]; Try it Yourself » Multiply all the values in an array with 10: const numbers = [65, 44, 12, 4]; function myFunction(num) { Try it Yourself » More examples below. Definition and Usage
Syntaxarray.map(function(currentValue, index, arr), thisValue) Parameters
Return Value
More ExamplesGet the full name for each person: const persons = [ persons.map(getFullName); function getFullName(item) { Try it Yourself » Browser Support
ES5 (JavaScript 2009) fully supported in all browsers:
How do you map an array of objects?How to use Javascript .. Simple iteration over array of objects.. Create new trasformed array.. Get list of values of a object property.. Add properties to every object entry of array.. Delete a property of every object entry in array.. Access key/value pairs of each object enty.. How do I map an array in JavaScript?The syntax for the map() method is as follows: arr. map(function(element, index, array){ }, this); The callback function() is called on each array element, and the map() method always passes the current element , the index of the current element, and the whole array object to it.
Can you map objects in JavaScript?Map is a collection of elements where each element is stored as a Key, value pair. Map object can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key, value pair in the same order as inserted.
How do you access an array of objects?A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };
|