javascript - Create JSON with through Excel Hierarchy -


i have table tree products. category, group , subgroup.

when export json excel file out without hierarchy: example:

        [   {     "id_cat": 4,     "desc cat": "acessorios para veiculos",     "id_gr": 1,     "desc gr": "acessorios nautica",     "id_sub": 15,     "desc sub": "bombas"   },   {     "id_cat": 4,     "desc cat": "acessorios para veiculos",     "id_gr": 1,     "desc gr": "acessorios nautica",     "id_sub": 16,     "desc sub": "cabos"   },   {     "id_cat": 4,     "desc cat": "acessorios para veiculos",     "id_gr": 1,     "desc gr": "acessorios nautica",     "id_sub": 17,     "desc sub": "helices"   }, 

must generate file this:

 [   {     "category": {       "id": 4,       "name": "acessorios para veiculos",       "group": [         {           "id": 1,           "name": "acessorios nautica",           "subgroup": [             {               "id": 15,               "name": "bombas"             },             {               "id": 16,               "name": "cabos"             },             {               "id": 17,               "name": "helices"             }           ]         },         {           "id": 2,           "name": "acessorios de carros",           "subgroup": [             {               "id": 26,               "name": "exterior"             },             {               "id": 27,               "name": "interior"             }           ]         }       ]     }   } ] 

the excel? know best way can create file through table format?

you can use js object hash table keep track of category id, group id , subgroup id.

i came quick implementation below (assuming items in initial array have id_cat, id_gr , id_sub value), have @ jsfiddle

function grouping(items) {      var cathash = {},          catlist = [],          = 0;                    (i = 0; < items.length; i++) {          var hash = cathash[items[i]["id_cat"]] || {};          hash.grouphash = hash.grouphash || {};                    var grouphash = hash.grouphash[items[i]["id_gr"]] || {};          grouphash.subgrouphash = grouphash.subgrouphash || {};                    var subgrouphash = grouphash.subgrouphash[items[i]["id_sub"]] || {},              cat = hash.category || {},              group = grouphash.group || {},              subgroup = subgrouphash.subgroup || {};                            if (!cat.id) {              cat.id = items[i]["id_cat"];              catlist.push(cat);              hash.category = cat;              cathash[cat.id] = hash;          }          if (!cat.name) {              cat.name = items[i]["desc cat"];          }          if (!cat.group) {              cat.group = [];          }            if (!group.id) {              group.id = items[i]["id_gr"];              cat.group.push(group);                            grouphash.group = group;              hash.grouphash[group.id] = grouphash;          }          if (!group.name) {              group.name = items[i]["desc gr"];          }          if (!group.subgroup) {              group.subgroup = [];          }                    if (!subgroup.id) {              subgroup.id = items[i]["id_sub"];              group.subgroup.push(subgroup);                            subgrouphash.subgroup = subgroup;              grouphash.subgrouphash[subgroup.id] = subgrouphash;          }          if (!subgroup.name) {              subgroup.name = items[i]["desc sub"];          }      }      return catlist;  }


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -