反转键的方法:对象中的值

无论如何,人们可能会转向以下;

{ "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>