【前端】pid2children iview tree json
<script> import inBody from '../inBody' export default { components:{ inBody } ,data () { return { data2: [ {title: 'parent 1'} ,{title: 'parent 1'} ] ,pidData:[ {id:"1",pid:"0",title:"一级目录"} ,{id:"2",pid:"0",title:"又是一个一级目录"} ,{id:"3",pid:"1",title:"二级栏目"} ] } } ,methods:{ pid2children:function(){ let pidData2 = this.pidData //pidData就是pid数据 //pidData2 === this.pidData ? console.info("true") : console.info("false") pidData2.forEach(el => { delete el.children //delete 删除数组 数组长度不变 特别适合 索引用 }) let map = {} pidData2.forEach(el => { map[el.id] = el // 循环data拿到所有的id }) let arr = [] pidData2.forEach(el => { let parent = map[el.pid] // 拿到所有的pid,如果为真,和id进行对比,如果pid===id,就说明是id的子集 if (parent) { (parent.children || (parent.children = [])).push(el) } else { // 如果不是就是第一级,没有pid或者pid为0 arr.push(el) console.log('arr', arr) } }) return arr; } } ,mounted:function(){ this.$nextTick(function () { // Code that will run only after the // entire view has been rendered //this.getData2() }) } } </script>
---------------------------------------------
生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
https://pengchenggang.gitee.io/navigator/
SMART原则:
目标必须是具体的(Specific)
目标必须是可以衡量的(Measurable)
目标必须是可以达到的(Attainable)
目标必须和其他目标具有相关性(Relevant)
目标必须具有明确的截止期限(Time-based)
生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
https://pengchenggang.gitee.io/navigator/
SMART原则:
目标必须是具体的(Specific)
目标必须是可以衡量的(Measurable)
目标必须是可以达到的(Attainable)
目标必须和其他目标具有相关性(Relevant)
目标必须具有明确的截止期限(Time-based)