i want to ask if it’s possible to do this:
JavaScript
x
8
1
const map1 = new Map();
2
map1.set('1', "led");
3
map1.set('2', "zeppelin");
4
5
const map2 = new Map();
6
map2.set('1', "led");
7
map2.set('2', "floyd");
8
I want to compare these 2 maps. I want to have a console.log() with the deferences of the maps.
Finally if the maps are the same and i add in map1 a new set
JavaScript
1
2
1
map1.set('3', "plant");
2
I want a log to tell me which is the new pair of kay-val
Thank you
Advertisement
Answer
JavaScript
1
23
23
1
const map1 = new Map();
2
map1.set('1', "led");
3
map1.set('2', "zeppelin");
4
5
const map2 = new Map();
6
map2.set('1', "led");
7
map2.set('2', "floyd");
8
9
let isSame = true;
10
map1.forEach(function(val, key){
11
if(map2.get(key) != val){
12
console.log('map1.'+key+' = '+val +' | map2.' + key + ' =
13
'+map2.get(key));
14
isSame = false;
15
}
16
})
17
if(isSame){
18
map1.set('3', "plant");
19
map1.forEach(function(val, key){
20
console.log('map1.'+key+' => '+val);
21
})
22
}
23