json - Converting array of Javascript objects to a Javascript array -


i have javascript object , when json.stringify it, looks following

[     {         "item_id": null,         "parent_id": "none",         "depth": 0,         "left": "1",         "right": 4     },     {         "item_id": "1",         "parent_id": null,         "depth": 1,         "left": 2,         "right": 3     } ] 

i want convert multi-dimensional array looks following

item[0][0] = item_id item[0][1] = parent_id item[0][2] = depth item[0][3] = left item[0][4] = right  item[1][0] = item_id item[1][1] = parent_id item[1][2] = depth item[1][3] = left item[1][4] = right 

any appreciated :)

edit : got working of :) help.

well lets take initial object (array) prior stringify. can loop each item. can create new array each property. this:

var myobject = x;//this original object var newarray = [];  for(var = 0; < myobject.length; i++){    var item = myobject[i];    var subarray = [];    subarray.push(item["item_id"]);    subarray.push(item["parent_id"]);    subarray.push(item["depth"]);    subarray.push(item["left"]);    subarray.push(item["right"]);    newarray.push(subarray); } 

here working example (check console result)

note: purposefully avoided using for in loop due rumours hear reliability of order. of course, if trust it's call, prefer play on safe side. you can read other opinions of matter here.


if want increase performance, create array directly values, so:

for (var = 0; < myobject.length; i++) {     var item = myobject[i];     var subarray = [item["item_id"], item["parent_id"], item["depth"], item["left"], item["right"]];     newarray.push(subarray); } 

this approximately twice fast performance wise, here proof


Comments