Suppose, we have an object like this −
const obj = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6 };
We are required to write a JavaScript function that takes in one such object. The function should reverse map the values to the keys of the object.
Therefore, for the above object, the output should look like −
const output = { '1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f' };
Example
const obj = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6 }; const reverseMap = (obj = {}) => { const res = {}; Object.keys(obj).forEach(key => { const val = obj[key]; res[val] = key; }); return res; }; console.log(reverseMap(obj));
Output
And the output in the console will be −
{ '1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f' }