shanmuga kumar s shanmuga kumar s - 2 months ago 8
Javascript Question

How to parse object to array one by one using jquery?

I have below json data,

{
"componants": [{
"category": "screensets",
"position": "top",
"rotate": "180",
"3d_file": "3d_deg_180.obj",
"height": "10",
"width": "10",
"x": "239",
"y": "186",
"current_roate": "0",
"comp_color": ""
}, {
"category": "screensets",
"position": "top",
"rotate": "180",
"3d_file": "3d_deg_180.obj",
"height": "10",
"width": "10",
"x": "619",
"y": "186",
"current_roate": "0",
"comp_color": ""
}, {
"category": "screensets",
"position": "top",
"rotate": "180",
"3d_file": "3d_deg_180.obj",
"height": "10",
"width": "10",
"x": "379",
"y": "86",
"current_roate": "0",
"comp_color": ""
}],
"width": "640",
"height": "640",
"name": "Test Drawing",
"size": "40",
"screen": "Conference set"
}


How to parse this value using jquery?

Answer

Are you try to manipulate your object to array like this below ?

[
  {
    "width"    : "640",
    "height"   : "640",
    "name"     : "Test Drawing",
    "size"     : "40",
    "screen"   : "Conference set",
    "category" : "screensets",
    "position" : "top",
    "rotate"   : "180",
    "3d_file"  : "3d_deg_180.obj",
    "height"   : "10",
    "width"    : "10",
    "x" : "379",
    "y" : "86",
    "current_roate" : "0",
    "comp_color"    : ""
  }

  ...
]

You can try this one :)

var obj = { ... }; // your object
obj = $.map(obj.componants,
  function(val, idx) {
    return $.extend(
      {  width : obj.width,
         height: obj.height,
         name  : obj.name,
         size  : obj.size,
         screen: obj.screen
      }, val);