Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript concat two Map objects [duplicate]

Is there any way to concat two (or more) Map Objects to a Map like array concat?

var map1 = new Map([['breakfast', 'meal'], ['dinner', 'meal']]);
var map2 = new Map([['launch', 'meal'], ['dinner', 'meal']]);
var concatenated = map1.concat(map2);
like image 901
Morteza Tourani Avatar asked Apr 30 '16 20:04

Morteza Tourani


1 Answers

The simplest way I found was to convert maps to key value pair array, concat them and finally create a new map from result:

var map1 = new Map([['breakfast', 'meal'], ['dinner', 'meal']]);
var map2 = new Map([['launch', 'meal'], ['dinner', 'meal']]);
var concatenated = new Map([...map1].concat([...map2]));
like image 164
Morteza Tourani Avatar answered Sep 22 '22 09:09

Morteza Tourani