javascript - CSV DATA import to nestable json data -


good evening.

i have csv data shown below.

level,label,price 1,menu1,3000 2,menu1_1,5000 2,menu1_2,6000 2,menu1_3,7000 1,menu2,8000 2,menu2_1,5000 3,menu2_1_1,5000 3,menu2_1_2,7000 2,menu2_2,6000 2,menu2_3,7000 1,menu3,9000 1,menu4,10000 

i want convert data json data shown below. necessary recursive form should write javascript code see?

[     {         "id": 1,         "label": "menu1",         "price": "3000",         "children": [             {                 "id": 2,                 "label": "menu1_1",                 "price": "5000",                 "children": []             },             {                 "id": 3,                 "label": "menu1_2",                 "price": "6000",                 "children": []             },             {                 "id": 4,                 "label": "menu1_1",                 "price": "7000",                 "children": []             }         ]     },     {         "id": 5,         "label": "menu2",         "price": "8000",         "children": [             {                 "id": 6,                 "label": "menu2_1",                 "price": "5000",                 "children": [                     {                         "id": 7,                         "label": "menu2_1_1",                         "price": "5000",                         "children": []                     },                     {                         "id": 8,                         "label": "menu2_1_2",                         "price": "7000",                         "children": []                     }                 ]             },             {                 "id": 9,                 "label": "menu2_2",                 "price": "6000",                 "children": []             },             {                 "id": 10,                 "label": "menu2_3",                 "price": "7000",                 "children": []             }         ]     },     {         "id": 11,         "label": "menu3",         "price": "9000",         "mnu_img": "",         "index": 46,         "children": []     },     {         "id": 12,         "label": "menu4",         "price": "10000",         "mnu_img": "",         "index": 50,         "children": []     } ] 

however, have transformation import csv source. note url: http://techslides.com/convert-csv-to-json-in-javascript

thanks answer. little problem shown below. enter image description here

i don't need array[0] please me.

first parse csv data js objects:

var array = data.split("\n").map(function(row, index) {    var arr = row.split(",")   return {     id: index+1,     level: +arr[0],     label: arr[1],     price: arr[2],     children: []   } }) 

then add level 0, has parent:

array.unshift({ level: 0, children: [] }) var parents = array.slice(0,1) 

i loop on data, comparing level parameter previous one. keep parents in stack, level 0 parent @ bottom.

for(var i=1; i<array.length; i++) {   var prev = array[i-1]   var obj = array[i]   var p = parents[parents.length-1]   if(obj.level > prev.level) {     parents.push(prev)     p = prev   } else if (obj.level < prev.level) {     {       p = parents.pop()     } while(obj.level < p.level)     p = parents[parents.length-1]   }   p.children.push(obj) } 

finally read out result

var result = parents[0].children console.log(result) 

working example: http://jsbin.com/zexiyigili/1/edit?js,console


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -