js 递归 更改数组的key值,针对ElementUI树形表格控件

   var tree=[
      {
        tename:'一级',
        teList:[
          {
            tename:'二级',
            teList:[
                {
                tename:'三级',
                teList:[
                  
                ]
              }
            ]
          },
          
        ]
      },
      {
        tename:'一yi级',
        teList:[
          {
            tename:'二级',
            teList:[
                {
                tename:'三级',
                teList:[
                  
                ]
              }
            ]
          },
          
        ]
      }
    ]
    var key = 'children'
    var keyname = 'name'
    function parseJson(arr) {
      arr = arr.slice()
      function toParse(arr) {
          arr.forEach(function (item) {
              if (item.teList && Array.isArray(item.teList)) {
                  item[key] = item.teList
                  item[keyname] = item.tename
                  toParse(item[key])
              }
              delete item.teList
              delete item.tename
          })
          return arr
        
      }
      return toParse(arr)
    }
  console.log(parseJson(tree))

你可能感兴趣的:(js 递归 更改数组的key值,针对ElementUI树形表格控件)