javascript 递归 tree

let animals = [
    { id: 'animals', name: 'animals', parent: null },
    { id: 'mammals', name: 'mammals', parent: 'animals' },
    { id: 'mammals2', name: 'mammals2', parent: 'animals' },
    { id: 'cats', name: 'cats', parent: 'mammals' },
    { id: 'dogs', name: 'dogs', parent: 'mammals' },
    { id: 'chihuahua', name: 'chihuahua', parent: 'dogs' },
    { id: 'labrador', name: 'labrador', parent: 'dogs' },
    { id: 'persian', name: 'persian', name: 'animals', parent: 'cats' },
    { id: 'siamese', name: 'siamese', parent: 'cats' }
]
let isEmptyObject = (obj) => {
    for (let t in obj)
        return !1;
    return !0
}
let makeTree = (animals, parent) => {
    let node = {}

    let filters = animals.filter(c => c.parent === parent)

    if (filters.length) {
        filters.forEach(c => {

            node[c.id] = c

            n = makeTree(animals, c.id)

            node[c.id]['children'] = []

            if (!isEmptyObject(n)) {
                node[c.id]['children'].push(n)
            }

        })
    }

    return node
}
console.log(
    JSON.stringify(
        makeTree(animals, null), null, 2
    )
)
/**
 {
  "animals": {
    "id": "animals",
    "name": "animals",
    "parent": null,
    "children": [
      {
        "mammals": {
          "id": "mammals",
          "name": "mammals",
          "parent": "animals",
          "children": [
            {
              "cats": {
                "id": "cats",
                "name": "cats",
                "parent": "mammals",
                "children": [
                  {
                    "persian": {
                      "id": "persian",
                      "name": "animals",
                      "parent": "cats",
                      "children": []
                    },
                    "siamese": {
                      "id": "siamese",
                      "name": "siamese",
                      "parent": "cats",
                      "children": []
                    }
                  }
                ]
              },
              "dogs": {
                "id": "dogs",
                "name": "dogs",
                "parent": "mammals",
                "children": [
                  {
                    "chihuahua": {
                      "id": "chihuahua",
                      "name": "chihuahua",
                      "parent": "dogs",
                      "children": []
                    },
                    "labrador": {
                      "id": "labrador",
                      "name": "labrador",
                      "parent": "dogs",
                      "children": []
                    }
                  }
                ]
              }
            }
          ]
        },
        "mammals2": {
          "id": "mammals2",
          "name": "mammals2",
          "parent": "animals",
          "children": []
        }
      }
    ]
  }
}
*/
    原文作者:QETHAN
    原文地址: https://segmentfault.com/a/1190000009078153
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞