指定元素的深度

时间:2016-09-28 15:08:45

标签: javascript arrays recursion depth

我有一组群组。每个组都有一个'groups'数组。它可以是空的,意味着该组没有子组,或者它可以具有其他组的索引,这些组是该组的子组。

var groups = [{
  "leaves": [],
  "groups": [1],
  "label": "A",
  "id": "5fe7a8ee-abd2-45d9-a79b-ff48e216f537",
  "parentGroup": null,
  "padding": 30
}, {
  "leaves": [],
  "groups": [2, 5, 8, 9],
  "label": "B",
  "id": "567e8de7-3aff-4b02-a891-b90a78bbf99f",
  "parentGroup": "5fe7a8ee-abd2-45d9-a79b-ff48e216f537",
  "padding": 30
}, {
  "leaves": [],
  "groups": [3, 4],
  "label": "C",
  "id": "56d73322-2601-477c-8e6f-dc3623cc37ed",
  "parentGroup": "567e8de7-3aff-4b02-a891-b90a78bbf99f",
  "padding": 30
}, {
  "leaves": [0],
  "groups": [],
  "label": "D",
  "id": "cc4e72f8-1844-4334-9796-60a0e1566331",
  "parentGroup": "56d73322-2601-477c-8e6f-dc3623cc37ed",
  "padding": 30
}, {
  "leaves": [1],
  "groups": [],
  "label": "E",
  "id": "0ffbc1a8-8ce3-4e37-bf4f-6f3283a5d8d2",
  "parentGroup": "56d73322-2601-477c-8e6f-dc3623cc37ed",
  "padding": 30
}, {
  "leaves": [],
  "groups": [6, 7],
  "label": "F",
  "id": "75b62737-0270-4569-8772-7d88eeffc801",
  "parentGroup": "567e8de7-3aff-4b02-a891-b90a78bbf99f",
  "padding": 30
}, {
  "leaves": [2],
  "groups": [],
  "label": "G",
  "id": "c69d6171-e834-4788-a206-64b7721a1022",
  "parentGroup": "75b62737-0270-4569-8772-7d88eeffc801",
  "padding": 30
}, {
  "leaves": [3],
  "groups": [],
  "label": "H",
  "id": "8f6b7a33-6156-4495-9ece-7acd16284ba8",
  "parentGroup": "75b62737-0270-4569-8772-7d88eeffc801",
  "padding": 30
}, {
  "leaves": [4, 5, 6, 7, 8],
  "groups": [],
  "label": "I",
  "id": "c4541ca8-c0f2-4694-a7a6-17092ad881ca",
  "parentGroup": "567e8de7-3aff-4b02-a891-b90a78bbf99f",
  "padding": 30
}, {
  "leaves": [9],
  "groups": [],
  "label": "J",
  "id": "f6f93345-991e-4759-8aaf-b65f016c4d8f",
  "parentGroup": "567e8de7-3aff-4b02-a891-b90a78bbf99f",
  "padding": 30
}, {
  "leaves": [],
  "groups": [11, 12, 13, 14],
  "label": "K",
  "id": "5d7de0ab-7949-412c-acc3-8e6f2f63b6ca",
  "parentGroup": null,
  "padding": 30
}, {
  "leaves": [10, 11],
  "groups": [],
  "label": "L",
  "id": "a997ab55-d8f2-436b-95e2-1635d98fdb5d",
  "parentGroup": "5d7de0ab-7949-412c-acc3-8e6f2f63b6ca",
  "padding": 30
}, {
  "leaves": [12],
  "groups": [],
  "label": "M",
  "id": "3cbaed7e-e3ca-4686-beb6-2599e56981c6",
  "parentGroup": "5d7de0ab-7949-412c-acc3-8e6f2f63b6ca",
  "padding": 30
}, {
  "leaves": [13],
  "groups": [],
  "label": "N",
  "id": "033b3242-6ff9-4bf0-833f-2042b9cb6978",
  "parentGroup": "5d7de0ab-7949-412c-acc3-8e6f2f63b6ca",
  "padding": 30
}, {
  "leaves": [14],
  "groups": [],
  "label": "O",
  "id": "a95821fb-31d9-4773-b35b-a330f60145b3",
  "parentGroup": "5d7de0ab-7949-412c-acc3-8e6f2f63b6ca",
  "padding": 30
}];

我希望,根据给定的数据,分配每个组的深度,从1开始,对于没有父组的所有组(parentGroup == null)。

我目前的做法是:

var mainGroups = groups.filter(function(group) {
  return group.parentGroup == null;
});

mainGroups.forEach(function(mainGroup) {
  var currentDepth = 1;
  mainGroup.depth = currentDepth;

  function assignDepths(group) {
    if (group.groups.length > 0) {
      currentDepth++;
      group.groups.forEach(function(subgroupIndex) {
        groups[subgroupIndex].depth = currentDepth;
      });
      group.groups.forEach(function(subgroupIndex) {
        assignDepths(groups[subgroupIndex]);
      });
    }
  }
  assignDepths(mainGroup);
});

然而,这是不正确的。 非常感谢帮助!

3 个答案:

答案 0 :(得分:1)

试试这个:



const groups = [
  {"leaves":[],"groups":[1],"label":"A","id":"5fe7a8ee-abd2-45d9-a79b-ff48e216f537","parentGroup":null,"padding":30}, 
  {"leaves":[],"groups":[2,5,8,9],"label":"B","id":"567e8de7-3aff-4b02-a891-b90a78bbf99f","parentGroup":"5fe7a8ee-abd2-45d9-a79b-ff48e216f537","padding":30}, 
  {"leaves":[],"groups":[3,4],"label":"C","id":"56d73322-2601-477c-8e6f-dc3623cc37ed","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30}, 
  {"leaves":[0],"groups":[],"label":"D","id":"cc4e72f8-1844-4334-9796-60a0e1566331","parentGroup":"56d73322-2601-477c-8e6f-dc3623cc37ed","padding":30}, 
  {"leaves":[1],"groups":[],"label":"E","id":"0ffbc1a8-8ce3-4e37-bf4f-6f3283a5d8d2","parentGroup":"56d73322-2601-477c-8e6f-dc3623cc37ed","padding":30}, 
  {"leaves":[],"groups":[6,7],"label":"F","id":"75b62737-0270-4569-8772-7d88eeffc801","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},
  {"leaves":[2],"groups":[],"label":"G","id":"c69d6171-e834-4788-a206-64b7721a1022","parentGroup":"75b62737-0270-4569-8772-7d88eeffc801","padding":30}, 
  {"leaves":[3],"groups":[],"label":"H","id":"8f6b7a33-6156-4495-9ece-7acd16284ba8","parentGroup":"75b62737-0270-4569-8772-7d88eeffc801","padding":30},
  {"leaves":[4,5,6,7,8],"groups":[],"label":"I","id":"c4541ca8-c0f2-4694-a7a6-17092ad881ca","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},
  {"leaves":[9],"groups":[],"label":"J","id":"f6f93345-991e-4759-8aaf-b65f016c4d8f","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},
  {"leaves":[],"groups":[11,12,13,14],"label":"K","id":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","parentGroup":null,"padding":30},
  {"leaves":[10,11],"groups":[],"label":"L","id":"a997ab55-d8f2-436b-95e2-1635d98fdb5d","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},
  {"leaves":[12],"groups":[],"label":"M","id":"3cbaed7e-e3ca-4686-beb6-2599e56981c6","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},
  {"leaves":[13],"groups":[],"label":"N","id":"033b3242-6ff9-4bf0-833f-2042b9cb6978","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},
  {"leaves":[14],"groups":[],"label":"O","id":"a95821fb-31d9-4773-b35b-a330f60145b3","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30}
]

const getDepth = (x, depth, arr) =>
  x.parentGroup === null
    ? depth
    : getDepth(arr.find(y => y.id === x.parentGroup), depth + 1, arr)

groups.forEach(x => x.depth = getDepth(x, 1, groups))

console.log(groups)




答案 1 :(得分:1)

问题在于,每次拨打assignDepths时,您都在递增currentDepth。在这一行:group.groups.forEach(function(subgroupIndex){ assignDepths(groups[subgroupIndex]); });,您在每个子组上调用assignDepths,这意味着每次循环移动到新的子组时,您正在递增currentDepth再次。这意味着如果你有2个子组应该处于相同的深度,那么由该循环处理的第二个子组将被分配比第一个更大的深度(如果第一个子组具有它自己的子组,则可能高几个级别)。

使用您提供的代码,一个简单的解决方法是使用当前组的深度来计算其子组的深度。试试这个:



var groups = [
  {"leaves":[],"groups":[1],"label":"A","id":"5fe7a8ee-abd2-45d9-a79b-ff48e216f537","parentGroup":null,"padding":30}, 
  {"leaves":[],"groups":[2,5,8,9],"label":"B","id":"567e8de7-3aff-4b02-a891-b90a78bbf99f","parentGroup":"5fe7a8ee-abd2-45d9-a79b-ff48e216f537","padding":30}, 
  {"leaves":[],"groups":[3,4],"label":"C","id":"56d73322-2601-477c-8e6f-dc3623cc37ed","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30}, 
  {"leaves":[0],"groups":[],"label":"D","id":"cc4e72f8-1844-4334-9796-60a0e1566331","parentGroup":"56d73322-2601-477c-8e6f-dc3623cc37ed","padding":30}, 
  {"leaves":[1],"groups":[],"label":"E","id":"0ffbc1a8-8ce3-4e37-bf4f-6f3283a5d8d2","parentGroup":"56d73322-2601-477c-8e6f-dc3623cc37ed","padding":30}, 
  {"leaves":[],"groups":[6,7],"label":"F","id":"75b62737-0270-4569-8772-7d88eeffc801","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},
  {"leaves":[2],"groups":[],"label":"G","id":"c69d6171-e834-4788-a206-64b7721a1022","parentGroup":"75b62737-0270-4569-8772-7d88eeffc801","padding":30}, 
  {"leaves":[3],"groups":[],"label":"H","id":"8f6b7a33-6156-4495-9ece-7acd16284ba8","parentGroup":"75b62737-0270-4569-8772-7d88eeffc801","padding":30},
  {"leaves":[4,5,6,7,8],"groups":[],"label":"I","id":"c4541ca8-c0f2-4694-a7a6-17092ad881ca","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},
  {"leaves":[9],"groups":[],"label":"J","id":"f6f93345-991e-4759-8aaf-b65f016c4d8f","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},
  {"leaves":[],"groups":[11,12,13,14],"label":"K","id":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","parentGroup":null,"padding":30},
  {"leaves":[10,11],"groups":[],"label":"L","id":"a997ab55-d8f2-436b-95e2-1635d98fdb5d","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},
  {"leaves":[12],"groups":[],"label":"M","id":"3cbaed7e-e3ca-4686-beb6-2599e56981c6","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},
  {"leaves":[13],"groups":[],"label":"N","id":"033b3242-6ff9-4bf0-833f-2042b9cb6978","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},
  {"leaves":[14],"groups":[],"label":"O","id":"a95821fb-31d9-4773-b35b-a330f60145b3","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30}
]

var mainGroups = groups.filter(function(group){return group.parentGroup == null;});

mainGroups.forEach(function(mainGroup){
    mainGroup.depth = 1;

    function assignDepths(group){
        if (group.groups.length>0){
            group.groups.forEach(function(subgroupIndex){ groups[subgroupIndex].depth = group.depth + 1; });
            group.groups.forEach(function(subgroupIndex){ assignDepths(groups[subgroupIndex]); });
        }
    }
    assignDepths(mainGroup);
});

console.log(groups)




答案 2 :(得分:1)

我想这应该是你想要的代码



function setDepth(a){
  return a.map(o => o.depth = o.groups.length ? Math.max(...o.groups.map(d => ++setDepth(o.groups.map(i => data[i]))[0])) : 0);
}
var data = [{"leaves":[],"groups":[1],"label":"A","id":"5fe7a8ee-abd2-45d9-a79b-ff48e216f537","parentGroup":null,"padding":30},{"leaves":[],"groups":[2,5,8,9],"label":"B","id":"567e8de7-3aff-4b02-a891-b90a78bbf99f","parentGroup":"5fe7a8ee-abd2-45d9-a79b-ff48e216f537","padding":30},{"leaves":[],"groups":[3,4],"label":"C","id":"56d73322-2601-477c-8e6f-dc3623cc37ed","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},{"leaves":[0],"groups":[],"label":"D","id":"cc4e72f8-1844-4334-9796-60a0e1566331","parentGroup":"56d73322-2601-477c-8e6f-dc3623cc37ed","padding":30},{"leaves":[1],"groups":[],"label":"E","id":"0ffbc1a8-8ce3-4e37-bf4f-6f3283a5d8d2","parentGroup":"56d73322-2601-477c-8e6f-dc3623cc37ed","padding":30},{"leaves":[],"groups":[6,7],"label":"F","id":"75b62737-0270-4569-8772-7d88eeffc801","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},{"leaves":[2],"groups":[],"label":"G","id":"c69d6171-e834-4788-a206-64b7721a1022","parentGroup":"75b62737-0270-4569-8772-7d88eeffc801","padding":30},{"leaves":[3],"groups":[],"label":"H","id":"8f6b7a33-6156-4495-9ece-7acd16284ba8","parentGroup":"75b62737-0270-4569-8772-7d88eeffc801","padding":30},{"leaves":[4,5,6,7,8],"groups":[],"label":"I","id":"c4541ca8-c0f2-4694-a7a6-17092ad881ca","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},{"leaves":[9],"groups":[],"label":"J","id":"f6f93345-991e-4759-8aaf-b65f016c4d8f","parentGroup":"567e8de7-3aff-4b02-a891-b90a78bbf99f","padding":30},{"leaves":[],"groups":[11,12,13,14],"label":"K","id":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","parentGroup":null,"padding":30},{"leaves":[10,11],"groups":[],"label":"L","id":"a997ab55-d8f2-436b-95e2-1635d98fdb5d","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},{"leaves":[12],"groups":[],"label":"M","id":"3cbaed7e-e3ca-4686-beb6-2599e56981c6","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},{"leaves":[13],"groups":[],"label":"N","id":"033b3242-6ff9-4bf0-833f-2042b9cb6978","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30},{"leaves":[14],"groups":[],"label":"O","id":"a95821fb-31d9-4773-b35b-a330f60145b3","parentGroup":"5d7de0ab-7949-412c-acc3-8e6f2f63b6ca","padding":30}];
setDepth(data);
console.log(data);