I just created two maps in the javascript by using below lines of code.
let myMap = new Map();
let myMap1 = new Map();
I just added the below data in the respective map.
//The below is just for example and the actual is different
myMap1 = new Map([
['country', 'Chile'],
['age', 30],
]);
myMap = new Map([
['country', 'Austria'],
['age', 20],
]);
There is a reason to keep the two map here because in the real time I’m trying to get the data from two different schema to store two different data in the Maps.
Now, I’m trying to iterate the both Map in the below order
Iteration-1 : country, Chile, country, Austria. //Index 0 from both the maps
Iteration-2 : age, 30 , age, 20. // Index 1 from both the maps.
I just want to iterate both the Maps in the single forEach and I want to take both index value and key and pass it for other logical operations.
Can someone assist me to solve this?
I tried the below way to iterate one Map but not sure how to do for two Maps
myMap.forEach (function(value, key) {
let array = key.split(":::");
htmlSkelton +=
"<tr>"+
"<th scope='row'>"+(counter++) +"</th>"+
"<td>"+array[1]+"</td>"+
"<td>"+array[0]+"</td>"+
"<td>"+value+"</td>"+
"</tr>"
})
3
Answers
Since both maps have the same keys, you can simply iterate over one map, and use its keys to access the other map.
Use string interpolation instead of concatenation to make the code more readable:
There are several ways to do this, see if this one fully suits you.
It seams
reduce
can be used here. Complexity is O(n), additional memory is required for an aggregate array: