combine two maps in javascript

To combine two maps in JavaScript, you can use the spread operator (...) to merge them into a single object. Here's an example:

index.tsx
const map1 = new Map();
map1.set('a', 1);
map1.set('b', 2);

const map2 = new Map();
map2.set('c', 3);
map2.set('d', 4);

const mergedMap = new Map([...map1, ...map2]);

console.log(mergedMap);
// Output: Map(4) { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 }
255 chars
13 lines

In this example, we create two maps map1 and map2, then combine them into a new map mergedMap using the spread operator. The resulting map contains all the key-value pairs from map1 and map2.

Note that if there are key collisions, the values from the second map will overwrite the values from the first map.

gistlibby LogSnag