无论如何,一个人可能会转向以下;
{
"ID": "id"
"Name": "name"
}
成;
{
"id": "ID",
"name": "Name"
}
用lodash?我特意寻找的东西;
var newObj = _.reverseMap(oldObj);
谢谢 :)
解决方法:
invert
适用于扁平对象,如果你想要它嵌套,你需要这样的东西:
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>