I am faced with a challenge involving a JavaScript map where all the values are the same. I need to create a new map for each unique value in the original map, storing key-value pairs that match. Essentially, I aim to create new maps based on the values present in the original map, with each value generating its own dedicated map where the keys remain the same.
//The mainMap is dynamically generated based on data retrieved from an API call
const mainMap = new Map();
//Populate the mainMap with data received from the API
//Sample data below; actual data is dynamic
mainMap.set(1, 'a');
mainMap.set(2, 'b');
mainMap.set(3, 'c');
mainMap.set(4, 'a');
mainMap.set(5, 'c');
mainMap.set(6, 'b');
mainMap.set(7, 'c');
mainMap.set(8, 'a');
mainMap.set(9, 'c');
mainMap.set(10, 'c');
mainMap.set(11, 'a');
mainMap.set(12, 'c');
mainMap.set(13, 'a');
mainMap.set(14, 'c');
mainMap.set(15, 'c');
mainMap.set(16, 'e');
mainMap.set(17, 'f');
......
//Identify keys with the same values and create new maps accordingly
const newMap1 = new Map();
newMap1.set(1, 'a')
newMap1.set(4, 'a');
newMap1.set(8, 'a');
newMap1.set(11, 'a');
newMap1.set(13, 'a');
const newMap2 = new Map();
newMap2.set(2, 'b');
newMap2.set(6, 'b');
const newMap3 = new Map();
newMap3.set(3, 'c');
newMap3.set(5, 'c');
newMap3.set(7, 'c');
newMap3.set(9, 'c');
newMap3.set(10, 'c');
newMap3.set(12, 'c');
newMap3.set(14, 'c');
newMap3.set(15, 'c');
const newMap4 = new Map();
newMap4.set(16, 'e');
const newMap5 = new Map();
newMap5.set(17, 'f');
......
Thank you in advance for your help!
I have attempted to split the main map into smaller sub-maps by identifying matching values, but have not been successful so far.