jsは配列をツリー構造に変換します.


jsは配列をツリー構造に変換します.
  • 1.データ構造
  • a.配列構造
  • b.ツリー構造
  • 例出力結果
  • .アルゴリズム実装
  • 1.データ構造
    a.配列構造
    [{
      //       
      name: Id,
      children: [Id],
    }]
    
    実例
    const arryData = [
      { name: 1, children: [ 2, 3 ] },
      { name: 2, children: [ 4, 5 ] },
      { name: 4, children: [ 7 ] },
      { name: 7, children: [ 8, 9 ] },
      { name: 8, children: [] },
      { name: 9, children: [] },
      { name: 5, children: [] },
      { name: 3, children: [ 6 ] },
      { name: 6, children: [] }
    ]
    
    b.ツリー構造
    const treeStructure = {
      name: Id, // name Id,   Number, String.    
      children: [treeStructure], // treeStructure   
      //       
    }
      
    
    インスタンス出力結果
    {
      name: 1,
      children: [{
        name: 2,
        children: [{
          name: 4,
          children: [{
            name: 7,
            children: [{
              name: 8,
              children: []
            }, {
              name: 9,
              children: []
            }]
          }],
        }, {
          name: 5,
          children: [],
        }]
      }, {
        name: 3,
        children: [{
          name: 6,
          children: [],
        }],
      }],
    }
    
    2.アルゴリズムの実現
    
    const arryData = [
      { name: 1, children: [ 2, 3 ] },
      { name: 2, children: [ 4, 5 ] },
      { name: 4, children: [ 7 ] },
      { name: 7, children: [ 8, 9 ] },
      { name: 8, children: [] },
      { name: 9, children: [] },
      { name: 5, children: [] },
      { name: 3, children: [ 6 ] },
      { name: 6, children: [] }
    ]
    
    //     transferArrayToTree  ,  arrayData   ,      treeData    
    
    // 2.      arrayData[idx]    
    //     return {
    //       name: arrayData[idx].name,
    //       children: [transferArrayToTree(idx, arrayData)]
    //     }
    
    //     ,         
    function transferArrayToTree(idx, arrayData) {
      let children = [], root = {}
      if (idx >= 0) {
        // console.log(idx)
        root.name = arrayData[idx].name
        root.children = arrayData[idx].children.map(value => {
          return transferArrayToTree(
            findChildrenIdxOfArray(value, arrayData),
            arrayData
          )
        })
        return root
      } else {
        return {}
      }
    }
    
    // name   
    function findChildrenIdxOfArray(name, arrayData) {
      return arrayData.findIndex(value => {
        return value.name === name
      })
    }
    
    //     name  children   
    function findRootIdxOfArray(arrayData) {
      let childrenNames = arrayData.reduce((pre, now) => {
        pre = pre.concat(now.children)
        return pre
      }, [])
      return arrayData.findIndex(value => {
        return childrenNames.indexOf(value.name) < 0
      })
    }
    
    console.log('root idx', findRootIdxOfArray(arrayData))
    const treeDataCopy = transferArrayToTree(0, arrayData)
    console.log(treeDataCopy)
    
    jsはツリー構造を行列に変換します.