js 实现了递归处理数据 - vue递归
一、基本操作
var param = { "org_name":"机构", "upload_time":20190304050708, "children":[ { "org_name":"机构1", "upload_time":20190304050708, "children":[ { "org_name":"机构11", "upload_time":20190304050808, "children":[ { "org_name": "机构111", "upload_time": 20190304050808 } ] } ] }, { "org_name": "机构2", "upload_time": 20190304050708, "children": [ { "org_name": "机构21", "upload_time": 20190304050808 } ] }, { "org_name": "机构3", "upload_time": 20190304050708, "children": [ { "org_name": "机构31", "upload_time": 20190304050808 } ] } ] }; function r(lst,pv){ for(var i in lst){ var obj = lst[i]; if(pv.length>0){ obj.sj=pv; obj.value= pv+ "0"+i; }else{ obj.value="01"; obj.sj="-"; } if(obj.children){ r(lst[i].children,obj.value); } } } var src = [param] r(src ,"") console.info(JSON.stringify(src[0]));
---------------------------------------------------------------------
二、数据结构的处理,递归,操作
数据结构1:
[ { "pid":"-1", "key":1558494360026, "type":"array", "sample":"er q y t", "name":"改天去天天", "order":1, "option":true, "paramType":"breq", "description":"q r y", "children":[ { "pid":1558494360026, "key":1558505884158, "type":"array", "sample":"22", "name":"11", "order":1, "option":"否", "paramType":"breq", "description":"33", "children":[ { "pid":1558505884158, "key":1558505891958, "type":"array", "sample":"33", "name":"22", "order":1, "option":"否", "paramType":"breq", "description":"44", "children":[ { "pid":1558505891958, "key":1558505898671, "type":"array", "sample":"44", "name":"33", "order":1, "option":"否", "paramType":"breq", "description":"55", "children":[ { "pid":1558505898671, "key":1558505906975, "type":"string", "sample":"55", "name":"44", "order":1, "option":"否", "paramType":"breq", "description":"66" }, { "pid":1558505898671, "key":1558505913679, "type":"string", "sample":"66", "name":"55", "order":2, "option":"否", "paramType":"breq", "description":"77" } ] } ] } ] } ] }, { "pid":"-1", "key":1558494331703, "type":"string", "sample":"t q t t", "name":"t q e t q", "order":2, "option":"否", "paramType":"breq", "description":"他天天" } ]
数据结构2:
[ { "sample":"er q y t", "name":"改天去天天", "children":{ "sample":"22", "name":"11", "children":{ "sample":"33", "name":"22", "children":{ "sample":"44", "name":"33", "children":[ { "sample":"55", "name":"44" }, { "sample":"66", "name":"55" } ] } } } }, { "sample":"t q t t", "name":"t q e t q" } ]
数据结构3:
[ { "改天去天天":{ "11":{ "22":{ "33":[ { "44":"55" }, { "55":"66" } ] } } } }, { "t q e t q":"t q t t" } ]
数据结构之间的转换:
methods: { func1(){ console.log("############################") var datalist = [ { "pid":"-1", "key":1558494360026, "type":"array", "sample":"er q y t", "name":"改天去天天", "order":1, "option":true, "paramType":"breq", "description":"q r y", "children":[ { "pid":1558494360026, "key":1558505884158, "type":"array", "sample":"22", "name":"11", "order":1, "option":"否", "paramType":"breq", "description":"33", "children":[ { "pid":1558505884158, "key":1558505891958, "type":"array", "sample":"33", "name":"22", "order":1, "option":"否", "paramType":"breq", "description":"44", "children":[ { "pid":1558505891958, "key":1558505898671, "type":"array", "sample":"44", "name":"33", "order":1, "option":"否", "paramType":"breq", "description":"55", "children":[ { "pid":1558505898671, "key":1558505906975, "type":"string", "sample":"55", "name":"44", "order":1, "option":"否", "paramType":"breq", "description":"66" }, { "pid":1558505898671, "key":1558505913679, "type":"string", "sample":"66", "name":"55", "order":2, "option":"否", "paramType":"breq", "description":"77" } ] } ] } ] } ] }, { "pid":"-1", "key":1558494331703, "type":"string", "sample":"t q t t", "name":"t q e t q", "order":2, "option":"否", "paramType":"breq", "description":"他天天" } ] console.log("datalist..src...",datalist) this.func2(datalist) console.log("datalist...",datalist) var lastVal = [] lastVal = datalist this.func3(lastVal) console.log("lastVal...",JSON.stringify(lastVal)) var ld = [] ld = lastVal this.func4(ld) console.log(JSON.stringify(ld)) console.log("############################") }, func2(datalist){ for(var i in datalist){ var obj = datalist[i] if(obj.children){ if(obj.children.length === 1){ obj.child = obj.children[0] } else{ obj.child = obj.children } this.func2(obj.children) } } }, func3(lastVal){ for(var i in lastVal){ var obj = lastVal[i] delete obj.description; delete obj.key; delete obj.option; delete obj.order; delete obj.key; delete obj.paramType delete obj.pid delete obj.type if(obj.children){ delete obj.children; if(obj.child.length){ this.func3(obj.child,lastVal) } else{ var t = [obj.child] this.func3(t,lastVal) } obj.children = obj.child delete obj.child } } }, func4(ld){ for(var i in ld){ var obj = ld[i] if(obj.name){ obj[obj.name] = obj.children ? obj.children : (obj.sample ? obj.sample : ""); } if(obj.children){ if(obj.children.length){ # if(obj.children instanceof Array){ 更好一些 this.func4(obj.children) } else{ var t = [obj.children] this.func4(t) } } delete obj.name delete obj.sample delete obj.children } } },
created(){
this.func1()
}