Javascript数组按级别排序

时间:2016-12-01 21:56:33

标签: javascript arrays sorting

我在同一个数组中有一对多关系的数据。该组织按级别建立。元素的父级总是比自身高一级,并由parentId引用。

您如何从此阵列获得多级数组?具有最高级别的元素将是主数组,其子项在javascript中是子数组?

[{
    _id: 100,
    level: 3,
    parentId: null,
},
{
    _id: 101,
    level: 2,
    parentId: 100,
},
{
    _id: 102,
    level: 2,
    parentId: 100,
},
{
    _id: 103,
    level: 2,
    parentId: 100,
},
{
    _id: 104,
    level: 1,
    parentId: 101,
},
{
    _id: 105,
    level: 1,
    parentId: 102,
},
{
    _id: 106,
    level: 1,
    parentId: 101,
},
{
    _id: 107,
    level: 1,
    parentId: 103,
},
{
    _id: 108,
    level: 1,
    parentId: 102,
},
{
    _id: 109,
    level: 1,
    parentId: 103,
}]

预期输出

                       100
                        |
       ------------------------------------
       |                |                 |
      101              102               103
    -------           ------            ------
    |     |           |    |            |    |
   104   106         105  108          107  109

由于

2 个答案:

答案 0 :(得分:0)

使用Array#reduce按节点的id创建哈希,然后使用Array.forEach()迭代数组。如果父ID为null,则它是根,如果不是将其添加到父级的子级中:



function createTree(data) {
  var tree = [];
  
  data.forEach(function(node) {
    var parentId = node.parentId;
    
    if(parentId === null) {
      tree.push(node);
    } else {
      (this[parentId].children || (this[parentId].children = [])).push(node);
    }
  }, data.reduce(function(hash, node) {
    hash[node._id] = node;
    
    return hash;
  }, Object.create(null)));
  
  return tree;
}

var data = [{
    _id: 100,
    level: 3,
    parentId: null,
},
{
    _id: 101,
    level: 2,
    parentId: 100,
},
{
    _id: 102,
    level: 2,
    parentId: 100,
},
{
    _id: 103,
    level: 2,
    parentId: 100,
},
{
    _id: 104,
    level: 1,
    parentId: 101,
},
{
    _id: 105,
    level: 1,
    parentId: 102,
},
{
    _id: 106,
    level: 1,
    parentId: 101,
},
{
    _id: 107,
    level: 1,
    parentId: 103,
},
{
    _id: 108,
    level: 1,
    parentId: 102,
},
{
    _id: 109,
    level: 1,
    parentId: 103,
}];

var result = createTree(data);

console.log(result);




答案 1 :(得分:0)

您可以使用单个循环从数据中创建一个树。此提案也适用于未分类的数据。



var data = [{ _id: 100, level: 3, parentId: null }, { _id: 101, level: 2, parentId: 100 }, { _id: 102, level: 2, parentId: 100 }, { _id: 103, level: 2, parentId: 100 }, { _id: 104, level: 1, parentId: 101 }, { _id: 105, level: 1, parentId: 102 }, { _id: 106, level: 1, parentId: 101 }, { _id: 107, level: 1, parentId: 103 }, { _id: 108, level: 1, parentId: 102 }, { _id: 109, level: 1, parentId: 103 }],
    tree = function (data, root) {
        var r, o = Object.create(null);
        data.forEach(function (a) {
            a.children = o[a._id] && o[a._id].children;
            o[a._id] = a;
            if (a.parentId === root) {
                r = a;
            } else {
                o[a.parentId] = o[a.parentId] || {};
                o[a.parentId].children = o[a.parentId].children || [];
                o[a.parentId].children.push(a);
            }
        });
        return r;
    }(data, null);

console.log(tree);

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