The original json
var json =
[{ "LABEL":"foo1", "DATA":340020, "BAR":235 },
{ "LABEL":"foo2", "DATA":140084, "BAR":330 },
{ "LABEL":"fooN", "DATA":126489, "BAR":120 }];
Below the desired format, where new DATA corresponds to old BAR
[{ "LABEL":"foo1", "DATA":235 },
{ "LABEL":"foo2", "DATA":330 },
{ "LABEL":"fooN", "DATA":120 }];
Nothing particularly fancy. I’d use a nested loop:
Key points of the above:
[]to create a new, blank array to assign tojBar.jsonarray using an old-fashioned index loop (notfor..in, that’s not what it’s for).{...}) to create the entries to put injBar.jBarby assignment to the next available slot. You could usejBar.push({...});instead, but surprisingly, it’s slower on several platforms and this is perfectly clear, so…