swap keys and values in object javascript code example
Example 1: swap key value object javascript
function swap(json){
var ret = {};
for(var key in json){
ret[json[key]] = key;
}
return ret;
}
Example 2: reverse keys and values in object javascript
var object = { 'a': 1, 'b': 2, 'c': 1 };
//This uses the package lodash
_.invert(object);
// => { '1': 'c', '2': 'b' }
// with `multiValue`
_.invert(object, true);
// => { '1': ['a', 'c'], '2': ['b'] }