Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何按id将对象数组组织到自身中_Javascript_Arrays - Fatal编程技术网

Javascript 如何按id将对象数组组织到自身中

Javascript 如何按id将对象数组组织到自身中,javascript,arrays,Javascript,Arrays,我有一个对象数组。这些对象可以是根元素或列表元素(树结构的元素)。如何循环该数组以将这些元素组织到深度为n(未知)的树中(对象具有其父元素-id的属性) 我正在尝试建立(菜单)。我已经成功地将列表元素组织到它们的父元素中。但我一直坚持将所有根元素放入它们的父元素(或根元素)。我遇到的问题是在没有嵌套循环的情况下进行(在所有内容都组织好的情况下进行检查) 起始数据示例: 如果objects parentGroupId为null,则表示它位于树的根目录中 目前我正在使用以下代码: for (var

我有一个对象数组。这些对象可以是根元素或列表元素(树结构的元素)。如何循环该数组以将这些元素组织到深度为n(未知)的树中(对象具有其父元素-id的属性)

我正在尝试建立(菜单)。我已经成功地将列表元素组织到它们的父元素中。但我一直坚持将所有根元素放入它们的父元素(或根元素)。我遇到的问题是在没有嵌套循环的情况下进行(在所有内容都组织好的情况下进行检查)

起始数据示例:

如果objects parentGroupId为null,则表示它位于树的根目录中

目前我正在使用以下代码:

for (var i = 0; i < groups.length; i++) {
  var childGroup = groups[i];

  if (childGroup.parentGroupId === null) {
    continue;
  }

  for (var j = 0; j < groups.length; j++) {
    var parentGroup = groups[j];

    if (childGroup.parentGroupId === parentGroup.groupId) {
      if (parentGroup.hasOwnProperty('children')) {
        parentGroup.children.push(childGroup);
      } else {
        parentGroup.children = [childGroup];
      }

      break;
    }
  }
}
for(变量i=0;i
使用和功能非常简单

const tree = groups.reduce((acc, group) => {
  group.children = groups.filter(g => g.parentGroupId === group.groupId);
  acc.push(group);
  return acc;
}, []).filter(g => g.parentGroupId === null);

有一种方法可以实现深度复制:

const testInput = [
    {
     "groupId": 1,
     "parentGroupId": null
    },
    {
     "groupId": 3,
     "parentGroupId": 1
    },
    {
      "groupId": 4,
      "parentGroupId": 3
    },
    {
      "groupId": 5,
      "parentGroupId": 1
    }
];

function flatToTree(flatList, idPropName, parentPropName, childrenPropName) {

    const list = [];
    const listIndex = {};

    /* Copy the array and create an index */

    flatList.forEach(listItem => {
      const objectCopy = Object.assign({}, listItem);
      list.push(objectCopy);
      listIndex[listItem[idPropName]] = objectCopy;
    });

    /* Assign children */

    list.forEach(listItem => {
      if (listItem[parentPropName]) {
        const parent = listIndex[listItem[parentPropName]];
        if (Array.isArray(parent[childrenPropName])) {
          parent[childrenPropName].push(listItem);
        }
        else {
          parent[childrenPropName] = [listItem];
        }
      }
    });

    /* Pick the root elements */

    return list.filter(listItem => listItem[parentPropName] === null);
}

const testOutput = flatToTree(testInput, "groupId", "parentGroupId", "children");

向我们展示到目前为止您尝试了什么。您的结果数组应该是什么样子的?两个提示:1)您只需要确保每个对象都添加到其父对象的子对象中。只需循环原始数组一次。2) 查找具有特定id(无循环)的对象的一种简单方法是首先构建对象映射id=>object(索引)。@ShubhamJain结果应该是有组织的数组(其父对象内的每个对象)。Pasbin中第13行前缺少一个右括号。此解决方案可能无法处理嵌套组。
const testInput = [
    {
     "groupId": 1,
     "parentGroupId": null
    },
    {
     "groupId": 3,
     "parentGroupId": 1
    },
    {
      "groupId": 4,
      "parentGroupId": 3
    },
    {
      "groupId": 5,
      "parentGroupId": 1
    }
];

function flatToTree(flatList, idPropName, parentPropName, childrenPropName) {

    const list = [];
    const listIndex = {};

    /* Copy the array and create an index */

    flatList.forEach(listItem => {
      const objectCopy = Object.assign({}, listItem);
      list.push(objectCopy);
      listIndex[listItem[idPropName]] = objectCopy;
    });

    /* Assign children */

    list.forEach(listItem => {
      if (listItem[parentPropName]) {
        const parent = listIndex[listItem[parentPropName]];
        if (Array.isArray(parent[childrenPropName])) {
          parent[childrenPropName].push(listItem);
        }
        else {
          parent[childrenPropName] = [listItem];
        }
      }
    });

    /* Pick the root elements */

    return list.filter(listItem => listItem[parentPropName] === null);
}

const testOutput = flatToTree(testInput, "groupId", "parentGroupId", "children");