jquery - Converting JSON object to javascript array -
i need convert json object:
"forms": { "circle": { "color" : "red", "size" : "1" }, "square": { "color" : "blue", "size" : "3" }, "triangle": { "color" : "black", "size" : "4" } }
into javascriptarray. array should contain formtype, color , size. have created following script
var formsarr=[]; $.each(forms, function(i, obj){ var form={color: obj.color, size: obj.size}; formsarr.push(form);
the array contains color , size. want containt formtype ie. circle, how can key?
here's way it:
var forms = { "circle": { "color" : "red", "size" : "1" }, "square": { "color" : "blue", "size" : "3" }, "triangle": { "color" : "black", "size" : "4" } }; var result = object.keys(forms).map(function(key) { return { type: key, color: this[key].color, size: this[key].size }; }, forms);
with jquery:
var result = $.map(forms, function(val, key) { return { type: key, color: val.color, size: val.size }; });
Comments
Post a Comment