Javascript swap function to swap map data
Following is the swap function in Javascript to change a map from k -> v to v -> k. If the data has duplicate values you will loose them.
function swap(json) {
var res = {};
for (var key in json) {
res[json[key]] = key;
}
return res;
}
Following is a full example with data to demonstrate the swap functionality
function swap(json) {
var res = {};
for (var key in json) {
res[json[key]] = key;
}
return res;
}
let data = {
"a": '1',
"b": '2',
"c": '3',
}
console.log(swap(data));
Output
{ '1': 'a', '2': 'b', '3': 'c' }
You can try this code online here https://onecompiler.com/javascript/3vy54pdge