jquery - Need help building a javascript array from given object values. -
this object have.
var myobject = { 'stop1-start': "0", 'stop1-color': "#0074a2", 'stop2-start': "32", 'stop2-color': "#ff6600" };
this array need.
var newarray =[ { 'stop-start': "0", 'stop-color': "#0074a2", }, { 'stop-start': "32", 'stop-color': "#ff6600", } ];
i tried loops, jquery each cant wrap head around it.
any appreciated.
object keys not guaranteed in order, you'll need find array's index within key itself:
var myobject = { 'stop1-start': "0", 'stop1-color': "#0074a2", 'stop2-start': "32", 'stop2-color': "#ff6600" }; var newarray= []; object.keys(myobject).sort().foreach(function(key) { var num= key.match(/(\d+)/)[0] - 1; newarray[num] = newarray[num] || {}; newarray[num][key.replace(num+1,'')]= myobject[key]; }); document.body.innerhtml= json.stringify(newarray);
Comments
Post a Comment