Last updated: Mar 4, 2024
Reading timeยท3 min
forEach()
reduce()
To convert an array of objects to a Map:
Array.map()
method to iterate over the array.Map()
constructor.const arr = [ {key: 'name', value: 'bobby hadz'}, {key: 'country', value: 'Chile'}, ]; const map1 = new Map( arr.map(obj => { return [obj.key, obj.value]; }), ); // ๏ธ๐๏ธ Map(2) { 'name' => 'bobby hadz', 'country' => 'Chile' } console.log(map1);
The function we passed to the Array.map() method gets called with each element (object) in the array.
On each iteration, we return an array containing the object's key and value.
The map()
method returns an array of key-value pairs.
const arr = [ {key: 'name', value: 'bobby hadz'}, {key: 'country', value: 'Chile'}, ]; // ๐๏ธ [ [ 'name', 'bobby hadz' ], [ 'country', 'Chile' ] ] console.log( arr.map(obj => { return [obj.key, obj.value]; }), );
The last step is to pass the result to the Map() constructor.
The Map()
constructor takes an iterable object, such as an array, as a
parameter and creates a new Map
object.
Note that the iterable must contain key-value pairs.
An alternative approach is to use the Array.forEach() method.
forEach()
This is a three-step process:
Map()
constructor to create a new Map
.Array.forEach()
method to iterate over the array.Map
.const arr = [ {key: 'name', value: 'bobby hadz'}, {key: 'country', value: 'Chile'}, ]; const map1 = new Map(); arr.forEach(obj => { map1.set(obj.key, obj.value); }); // ๏ธ๐๏ธ { 'name' => 'bobby hadz', 'country' => 'Chile' } console.log(map1);
The function we passed to the forEach()
method gets called with each element
(object) in the array.
forEach
method returns undefined
, so we have to declare a variable that will store the state.On each iteration, we use the
Map.set() method to add a new
key-value pair to the Map
.
reduce()
This is a three-step process:
Array.reduce()
method to iterate over the array.Map
.Map
object.const arr = [ {key: 'name', value: 'bobby hadz'}, {key: 'country', value: 'Chile'}, ]; const map1 = arr.reduce((accumulator, obj) => { return accumulator.set(obj.key, obj.value); }, new Map()); // ๐๏ธ Map(2) { 'name' => 'bobby hadz', 'country' => 'Chile' } console.log(map1);
The function we passed to the Array.reduce() method gets called for each element (object) in the array.
We initialized the accumulator
variable to a new Map
object because that's
what we passed as the second argument to the reduce()
method.
On each iteration, we use the Map.set()
method to add the current key-value
pair to the Map
object.
The Map.set() method adds or
updates an entry in a Map
with the specified key and value.
The method takes the key
and value
that should be added to the Map
as
parameters.
Use the Map()
constructor to convert a two-dimensional array to a Map
object.
The Map
constructor takes a two-dimensional array and returns a new Map
object with all of the specified keys and values added to it.
const arr = [ ['name', 'bobby hadz'], ['country', 'Chile'], ]; const map1 = new Map(arr); // ๐๏ธ Map(2) { 'name' => 'bobby hadz', 'country' => 'Chile' } console.log(map1);
We passed a two-dimensional array containing key-value pairs directly to the
Map() constructor. All key-value
pairs got added to the new Map
.
You can learn more about the related topics by checking out the following tutorials: