javascript - Simplest way to convert a JSON object to a newly structured JSON object? -
i've got json object structured this:
{ "xaxis": [ "foo", "bar", "baz" ], "yaxis": [ 333, 992, 1365 ] }
from i'd create json object stuctured this:
{ "piegraph": [ ["foo",333], ["bar",992], ["baz",1365] ] }
doing conversion in client-side javascript save me additional development , server round-trip fetch same data.
i can employ jquery library if help.
assuming first json string parsed object, have iterate on elements of of 2 arrays, build result:
var result = { piegraph: [] }; // initialize piegraph empty array var l = obj.xaxis.length; while(l--) { result.piegraph[l] = [ obj.xaxis[l], obj.yaxis[l] ]; } // result this: // {"piegraph":[["foo",333],["bar",992],["baz",1365]]}
no libraries needed, plain sequential loop. ;)
Comments
Post a Comment