javascript - revert object and its properties -
i have object
var obj = { prop1 : { dummy1 : 0, dummy2 : 0, dummy3 : 0 }, prop2 : { dummy1 : 0, dummy2 : 0, dummy3 : 0 } };
and need revert dummy becomes object , props becomes it's properties.
so need object this
var obj = { dummy1 : { prop1 : 0, prop2 : 0 }, dummy2 : { prop1 : 0, prop2 : 0 }, dummy3 : { prop1 : 0, prop2 : 0 } };
sorry if stupid question, i'm tired , stuck on this. every appreciated , reworded.
this achievable 2 for..in
loops
var obj = { prop1 : {dummy1 : 0, dummy2 : 0, dummy3 : 0}, prop2 : {dummy1 : 0, dummy2 : 0, dummy3 : 0} }; // start of interesting code var a, b, o2 = {}; (a in obj) (b in obj[a]) { if (!o2[b]) o2[b] = {}; // if o2 doesn't have yet, make new obj o2[b][a] = obj[a][b]; // swap } o2; /* { "dummy1": {"prop1": 0, "prop2": 0}, "dummy2": {"prop1": 0, "prop2": 0}, "dummy3": {"prop1": 0, "prop2": 0} } */
you may want check properties not inherited, etc.
Comments
Post a Comment