首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用lodash.js实现平面数组到树数组

使用lodash.js实现平面数组到树数组
EN

Stack Overflow用户
提问于 2016-04-19 13:34:32
回答 4查看 3.3K关注 0票数 4

我试图将平面数组转换为树数组,因为我将使用jsTree中的数据。此外,我需要转换的关键名称,如“名称”为“文本”。

我想使用lodash.js,但实际上我是房客。我寻找了解决办法,但找不到适合我的情况。

你能帮上忙吗?我的平面数组数据如下:

代码语言:javascript
复制
[
    {
        Id:1,
        Name: 'name1',
        Parent: 0
    },
    {
        Id:2,
        Name: 'name2',
        Parent: 1
    },
    {
        Id:3,
        Name: 'name3',
        Parent: 2
    },
    {
        Id:4,
        Name: 'name4',
        Parent: 1
    },
    {
        Id:5,
        Name: 'name5',
        Parent: 1
    },
    {
        Id:6,
        Name: 'name6',
        Parent: 5
    }
]

我希望拥有这样的树数据:

代码语言:javascript
复制
{
    "id": 1, 
    "text" : "name1", 
    "children" : [
        { 
            "id": 2, 
            "text" : "name2", 
            "children" : [{
                "id": 3,
                "text": "name3"
            }] 
        },
        { 
            "id": 4, 
            "text" : "name4" 
        },
        { 
            "id": 5, 
            "text" : "name5",
            "children" : [{
                "id": 6,
                "text": "name6"
            }]  
        }
    ]
}

提前谢谢你

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-04-19 13:51:26

这是普通Javascript中关于未排序数据的建议。

代码语言:javascript
复制
var data = [{ Id: 1, Name: 'name1', Parent: 0 }, { Id: 2, Name: 'name2', Parent: 1 }, { Id: 3, Name: 'name3', Parent: 2 }, { Id: 4, Name: 'name4', Parent: 1 }, { Id: 5, Name: 'name5', Parent: 1 }, { Id: 6, Name: 'name6', Parent: 5 }],
    tree = function (data, root) {
        var r;
        data.forEach(function (a) {
            this[a.Id] = { id: a.Id, text: a.Name, children: this[a.Id] && this[a.Id].children };
            if (a.Parent === root) {
                r = this[a.Id];
            } else {
                this[a.Parent] = this[a.Parent] || {};
                this[a.Parent].children = this[a.Parent].children || [];
                this[a.Parent].children.push(this[a.Id]);
            }
        }, Object.create(null));
        return r;
    }(data, 0);

document.write('<pre>' + JSON.stringify(tree, 0, 4) + '</pre>');

票数 2
EN

Stack Overflow用户

发布于 2016-04-19 13:55:07

如果您不确定子是否不能出现在父级之前,则可以使用以下代码:

代码语言:javascript
复制
var t = [{ Id: 1, Name: 'name1', Parent: 0 }, { Id: 2, Name: 'name2', Parent: 1 }, { Id: 3, Name: 'name3', Parent: 2 }, { Id: 4, Name: 'name4', Parent: 1 }, { Id: 5, Name: 'name5', Parent: 1 }, { Id: 6, Name: 'name6', Parent: 5 }];

var elements = [];
t.forEach(function(element) {
	elements[element.Id] = {
		id: element.Id,
		text: element.Name,
		parent: element.Parent,
		children: []
	}
});

elements.forEach(function(element) {
	elements[element.parent] && elements[element.parent].children.push(element);
	delete element.parent;
})

document.write(['<pre>', JSON.stringify(elements[1], 0, 3), '</pre>'].join(''));

票数 1
EN

Stack Overflow用户

发布于 2016-07-16 13:18:16

下面的函数从对象列表构建树。它不紧于任何格式。

代码语言:javascript
复制
function buildTree(flatList, idFieldName, parentKeyFieldName, fieldNameForChildren) {
    var rootElements = [];
    var lookup = {};

    flatList.forEach(function (flatItem) {
      var itemId = flatItem[idFieldName];
      lookup[itemId] = flatItem;
      flatItem[fieldNameForChildren] = [];
    });

    flatList.forEach(function (flatItem) {
      var parentKey = flatItem[parentKeyFieldName];
      if (parentKey != null) {
        var parentObject = lookup[flatItem[parentKeyFieldName]];
        if(parentObject){
          parentObject[fieldNameForChildren].push(flatItem);
        }else{
          rootElements.push(flatItem);
        }
      } else {
        rootElements.push(flatItem);
      }

    });

    return rootElements;
  }

这是一把小提琴使用您的示例作为输入。

原始源来自于这个答案

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36720068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档