Anyway one might be able to turn the following;
{
"ID": "id"
"Name": "name"
}
into;
{
"id": "ID",
"name": "Name"
}
Using lodash? I'm specifically looking for something along the lines of;
var newObj = _.reverseMap(oldObj);
Thanks :)
invert
works fine for flat objects, if you want it to be nested, you need something like this:
var deepInvert = function(obj) {
return _.transform(obj, function(res, val, key) {
if(_.isPlainObject(val)) {
res[key] = deepInvert(val);
} else {
res[val] = key;
}
});
};
//
var a = {
x: 1,
y: 2,
nested: {
a: 8,
b: 9
}
};
var b = deepInvert(a);
document.write('<pre>'+JSON.stringify(b,0,3));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.2.0/lodash.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With