javascript - 使用无序数组的分层树

标签 javascript typescript

我有一个无序数组。 id 是唯一值。parent 是父级的id。我需要一个分层 JSON。

var C=[
        {
        "id": 57,
        "name": "Breaded Chicken Items",
        "slug": "breaded-chicken-items",
        "parent": 55,
        "description": "",
        "display": "default",
        "image": "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-2.jpg",
        "count": 0
        },
        {
        "id": 70,
        "name": "Curry Masala Blends",
        "slug": "curry-masala-blends",
        "parent": 69,
        "description": "",
        "display": "default",
        "image": "http://coitor.com/emke/wp-content/uploads/2016/04/purto-probatus.jpg",
        "count": 0
        },
        {
        "id": 55,
        "name": "Fish | Meat | Frozen Veg",
        "slug": "fish-meat-frozen-veg",
        "parent": 0,
        "description": "",
        "display": "default",
        "image": "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-2.jpg",
        "count": 0
        },
        {
        "id": 186,
        "name": "Frozen Veg",
        "slug": "frozen-veg",
        "parent": 55,
        "description": "",
        "display": "default",
        "image": "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-1.jpg",
        "count": 0
        },
        {
        "id": 69,
        "name": "Spices | Curry Powders",
        "slug": "spices-curry-powders",
        "parent": 0,
        "description": "",
        "display": "default",
        "image": "http://coitor.com/emke/wp-content/uploads/2016/04/rsz_birds_eye_chilli.jpg",
        "count": 0
        },
        {
        "id": 47,
        "name": "Vegetables",
        "slug": "vegetables",
        "parent": 0,
        "description": "",
        "display": "subcategories",
        "image": "http://coitor.com/emke/wp-content/uploads/2016/04/wisi-antiopam.jpg",
        "count": 15
        },
        {
        "id": 72,
        "name": "Whole Spices",
        "slug": "whole-spices",
        "parent": 69,
        "description": "",
        "display": "default",
        "image": "http://coitor.com/emke/wp-content/uploads/2016/04/fresh-greenpeas.jpg",
        "count": 0
        }
        ]

喜欢这个输出

  [{
    "id": 55,
    "name": "Fish | Meat | Frozen Veg",
    "slug": "fish-meat-frozen-veg",
    "parent": 0,
    "description": "",
    "display": "default",
    "image": "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-2.jpg",
    "count": 0
    "Children":[
         {
            "id": 57,
            "name": "Breaded Chicken Items",
            "slug": "breaded-chicken-items",
            "parent": 55,
            "description": "",
            "display": "default",
            "image": "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-2.jpg",
            "count": 0
         }
     ]
   }]..etc

我不知道。

最佳答案

您可以通过使用 idparent 的信息来使用单循环方法来生成对象中的节点。

如果找到没有父节点的节点,则该节点是根节点并添加到结果数组中。

var data = [{ id: 57, name: "Breaded Chicken Items", slug: "breaded-chicken-items", parent: 55, description: "", display: "default", image: "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-2.jpg", count: 0 }, { id: 70, name: "Curry Masala Blends", slug: "curry-masala-blends", parent: 69, description: "", display: "default", image: "http://coitor.com/emke/wp-content/uploads/2016/04/purto-probatus.jpg", count: 0 }, { id: 55, name: "Fish | Meat | Frozen Veg", slug: "fish-meat-frozen-veg", parent: 0, description: "", display: "default", image: "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-2.jpg", count: 0 }, { id: 186, name: "Frozen Veg", slug: "frozen-veg", parent: 55, description: "", display: "default", image: "http://coitor.com/emke/wp-content/uploads/2016/05/mainbanner-1.jpg", count: 0 }, { id: 69, name: "Spices | Curry Powders", slug: "spices-curry-powders", parent: 0, description: "", display: "default", image: "http://coitor.com/emke/wp-content/uploads/2016/04/rsz_birds_eye_chilli.jpg", count: 0 }, { id: 47, name: "Vegetables", slug: "vegetables", parent: 0, description: "", display: "subcategories", image: "http://coitor.com/emke/wp-content/uploads/2016/04/wisi-antiopam.jpg", count: 15 }, { id: 72, name: "Whole Spices", slug: "whole-spices", parent: 69, description: "", display: "default", image: "http://coitor.com/emke/wp-content/uploads/2016/04/fresh-greenpeas.jpg", count: 0 }],
    tree = function (data, root) {
        var r = [], o = {};
        data.forEach(function (a) {
            a.children = o[a.id] && o[a.id].children;
            o[a.id] = a;
            if (a.parent === root) {
                r.push(a);
                return;
            }
            o[a.parent] = o[a.parent] || {};
            o[a.parent].children = o[a.parent].children || [];
            o[a.parent].children.push(a);
        });
        return r;
    }(data, 0);

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 使用无序数组的分层树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45479472/

相关文章:

javascript - 根据 React 组件的 props 改变其样式

Javascript:控制台日志记录

node.js - npm @types 包的类型 globalDevDependency 的等价物是什么?

具有名为 "new"的函数的 Typescript 接口(interface)

javascript - 'referral links' 是如何被发现的?

javascript - jQuery UI Datepicker 未安装

json - Angular 4 - 将 JSON 映射到模型,反之亦然

node.js - 使用 Stackblitz 声明依赖版本?

javascript - 获取表内每个表的数据内容

angular - 仅当第一个 Observable 返回 null 时如何调用第二个 Observable