递归数组,给最里层的数据添加节点

data () {
    
    
  return {
    
    
    list: [{
    
    
      name: 'xiaoming',
      id: 1,
      children: [
        {
    
    
          name: 'xiaoming2',
          id: 2,
          children: []
        },
        {
    
    
          name: 'xiaoming3',
          id: 3,
          children: [
            {
    
    
              name: 'xiaoming5',
              id: 5
            },
            {
    
    
              name: 'xiaoming5',
              id: 5
            }
          ]
        }
      ]
    }]
  }
},
methods: {
    
    
 digui (arr) {
    
    
    arr.forEach(v => {
    
    
      if (v.children instanceof Array) {
    
    
        this.digui(v.children)
      } else {
    
    
        v.children = {
    
    
          text: 'haha',
          text2: 'xixi'
        }
      }
    });
  }
},
mounted () {
    
    
  this.digui(this.list);
  console.log(this.list)
}

猜你喜欢

转载自blog.csdn.net/yuyu_2019/article/details/114700336