JS: Swap Key Value of Map 🚀
Here is a function that reverse the key/value of map object. That is, value become key, vice versa.
/* xah_inverse_map(xmap) return a new map object that has key and val of xmap swapped. URL: http://xahlee.info/js/js_map_reverse_key_val.html Version 2020-04-21 2022-09-17 */ const xah_inverse_map = ((xmap) => { const xm2 = new Map(); xmap.forEach((vv, kk) => { xm2.set(vv, kk); }); return xm2; }); // ssss--------------------------------------------------- // test console.log( xah_inverse_map(new Map([[1, "a"], [2, "b"]])), ); // Map { "a" => 1, "b" => 2 }
JavaScript, Reverse Key Value
JavaScript, Map Object
- JS: Map Object Tutorial
- JS: Difference, Object vs Map
- JS: Literal Expression for Map
- JS: Nested Map
- JS: Iterate Map Object
- JS: Convert Object to Map 🚀
- JS: Convert Map to Object 🚀
- JS: Map Equality 🚀
- JS: Map Filter 🚀
- JS: Swap Key Value of Map 🚀
- JS: Sort Map 🚀
- JS: Truncate Map 🚀
- JS: Map Substract 🚀
- JS: How Map Determines Uniqueness of Keys
- JS: Map Object
- JS: Map Constructor
- JS: Map.prototype