使用 JavaScript 查找第 N 个 children 并想要创建 Treeview

Find Nth number of children using JavaScript and want to create Treeview

我有这个哈希数组,想将它递归到树视图中,直到 ParentTradeFairResourceId 存在。

我已经尝试创建算法但没有成功。

这里附上代码和我的作品。

var newArr = [{
    "id": "39",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Yarn",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "15",
    "FairCategoryUrl": "#",
    "DisplayOrder": "3",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "40",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Apparel",
    "IconUrl": "apparel-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "2",
    "FairCategoryUrl": "#",
    "DisplayOrder": "1",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "41",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Fabric",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "6",
    "FairCategoryUrl": "#",
    "DisplayOrder": "2",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "42",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Allied",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "17",
    "FairCategoryUrl": "#",
    "DisplayOrder": "4",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "57",
    "tradefairid": "55964",
    "ResourceId": "26",
    "ResourceImageDetailId": "21",
    "Name": "Exhibitors",
    "IconUrl": "/header/apparel.svg",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "1",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "58",
    "tradefairid": "55964",
    "ResourceId": "27",
    "ResourceImageDetailId": "22",
    "Name": "Corporate Videos",
    "IconUrl": "/header/fabric.svg",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "6",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "59",
    "tradefairid": "55964",
    "ResourceId": "28",
    "ResourceImageDetailId": "23",
    "Name": "Fair Lobby",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "60",
    "tradefairid": "55964",
    "ResourceId": "29",
    "ResourceImageDetailId": "46",
    "Name": "Auditorium",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "2",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "61",
    "tradefairid": "55964",
    "ResourceId": "32",
    "ResourceImageDetailId": "23",
    "Name": "Networking",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "4",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "62",
    "tradefairid": "55964",
    "ResourceId": "34",
    "ResourceImageDetailId": "26",
    "Name": "Knowledge",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "3",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "63",
    "tradefairid": "55964",
    "ResourceId": "35",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "7",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "64",
    "tradefairid": "55964",
    "ResourceId": "36",
    "Name": "About Organizor",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "8",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "65",
    "tradefairid": "55964",
    "ResourceId": "37",
    "Name": "About the Event",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "9",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "66",
    "tradefairid": "55964",
    "ResourceId": "41",
    "Name": "Footer Exhibitor Directory",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "67",
    "tradefairid": "55964",
    "ResourceId": "42",
    "Name": "F2F Services",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "10",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "68",
    "tradefairid": "55964",
    "ResourceId": "43",
    "Name": "Contact US",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "11",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "69",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Category List",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "0",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "IsMandatory": "0"
  },
  {
    "id": "70",
    "tradefairid": "55964",
    "ResourceId": "35",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "0",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "5",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "71",
    "tradefairid": "55964",
    "ResourceId": "49",
    "ResourceImageDetailId": "36",
    "Name": "Exhibitors",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "3",
    "IsMandatory": "0"
  },
  {
    "id": "72",
    "tradefairid": "55964",
    "ResourceId": "50",
    "ResourceImageDetailId": "35",
    "Name": "Auditorium",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "2",
    "IsMandatory": "0"
  },
  {
    "id": "73",
    "tradefairid": "55964",
    "ResourceId": "51",
    "ResourceImageDetailId": "34",
    "Name": "Knowledge",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "1",
    "IsMandatory": "0"
  },
  {
    "id": "74",
    "tradefairid": "55964",
    "ResourceId": "52",
    "ResourceImageDetailId": "37",
    "Name": "Networking",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "4",
    "IsMandatory": "0"
  },
  {
    "id": "75",
    "tradefairid": "55964",
    "ResourceId": "35",
    "ResourceImageDetailId": "38",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "5",
    "IsMandatory": "0"
  },
  {
    "id": "79",
    "tradefairid": "55964",
    "ResourceId": "56",
    "ResourceImageDetailId": "45",
    "Name": "Booth Detail",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMandatory": "0"
  },
  {
    "id": "95",
    "tradefairid": "55964",
    "ResourceId": "67",
    "Name": "Signup",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "96",
    "tradefairid": "55964",
    "ResourceId": "69",
    "Name": "Name",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "97",
    "tradefairid": "55964",
    "ResourceId": "70",
    "Name": "Designation",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "98",
    "tradefairid": "55964",
    "ResourceId": "71",
    "Name": "Company",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "99",
    "tradefairid": "55964",
    "ResourceId": "72",
    "Name": "Business Category",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "100",
    "tradefairid": "55964",
    "ResourceId": "73",
    "Name": "Email",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "101",
    "tradefairid": "55964",
    "ResourceId": "74",
    "Name": "Mobile",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "102",
    "tradefairid": "55964",
    "ResourceId": "75",
    "Name": "City",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "104",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "103",
    "IsMandatory": "0"
  },
  {
    "id": "105",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "104",
    "IsMandatory": "0"
  },
  {
    "id": "106",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "104",
    "IsMandatory": "0"
  },
  {
    "id": "107",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "105",
    "IsMandatory": "0"
  }
];

function main(newArr) {
  let hasParentResourceId = newArr.filter((word) => word.ParentTradeFairResourceId);
  let hasNotParentResourceId = newArr.filter((word) => !word.ParentTradeFairResourceId);
  console.log(hasParentResourceId);
  console.log(hasNotParentResourceId);
  var countList = hasParentResourceId.reduce((p, c) => {
    p[c.ParentTradeFairResourceId] = (p[c.ParentTradeFairResourceId] || 0) + 1;
    return p;
  }, {});
  var createNewArrayForCountList = [];
  for (const property in countList) {
    createNewArrayForCountList.push({
      id: property
    });
  }
  console.log(countList);
  var mapMainArray = [];
  console.log(createNewArrayForCountList);
  setTimeout(() => {
    for (let index = 0; index < createNewArrayForCountList.length; index++) {
      var createNewArrr = [];
      for (let j = 0; j < hasParentResourceId.length; j++) {
        if (createNewArrayForCountList[index]["id"] == hasParentResourceId[j]['ParentTradeFairResourceId']) {
          createNewArrr.push(hasParentResourceId[j]);
          if (hasParentResourceId.length - 1) {
            hasParentResourceId[j]['childElements'] = createNewArrr;
          }
        }
      }
      // mapMainArray.push({ 'childElements': createNewArrr });
      // console.log(mapMainArray);
    }
  }, 500);
}
main(newArr);

循序渐进

首先我要说我希望这有用,但你的问题并不完全清楚,我不完全确定你说的是什么意思

want to recursive this into treeview upto ParentTradeFairResourceId exist.

我收集到的是,您的每个数组 object 都可以有一个 parent,并且您想要一个嵌套每个 children 的 object在 parent 下递归,所以你会有多个级别。让我们先选择那些位于根目录的 object(没有 parent):

// Get the list of all roots
let roots = newArr.filter(e => !e.ParentTradeFairResourceId);

然后,对于每个根,我们要构建树。为此,我们将向每个根添加一个 children 列表 属性,并将其所有 children 放在那里。我们将创建一个简单的函数来执行此操作:

function connectChildren(root, arr) {
    root.children = [];

    // connect the children from the array to the root
    for (child of arr) {
        if (child.ParentTradeFairResourceId == root.id) {
            root.children.push(child);
        }
    };
    
    return root;
}

现在,我们可以使用我们的 roots 数组并为每个 child:

调用我们的函数
// Get the list of all roots
let roots = newArr.filter(e => !e.ParentTradeFairResourceId);

// Attach children to each root
for (let root of roots) {
    root = connectChildren(root, newArr);
}

这一切都很好,我们可以看到 children 数组填满了正确的 child 值。但是,这仅适用于 1 级(仅附加根的直接 children)。

我们需要更深入

为了解决这个问题,我们可以在我们的函数中对每个 children 进行递归调用,将它们用作新的根,并附加它们的 children:

function connectChildren(root, arr) {
    root.children = [];

    // connect the children from the array to the root
    for (let child of arr) {
        if (child.ParentTradeFairResourceId == root.id) {
            root.children.push(child);
        }
    }
    
    // for each child, construct the tree in this same manner
    for (let child of root.children) {
        child = connectChildren(child, arr);
    }

    return root;
}

注意我们每次都传递完整的数组,所以如果数组定义中有任何循环,这将不起作用。一旦将项目添加到树中,您可以通过向后循环并从 arr 中删除项目来缓解这种情况,但这通常可以正常工作。

可运行代码示例:

let newArr = [{
    "id": "39",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Yarn",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "15",
    "FairCategoryUrl": "#",
    "DisplayOrder": "3",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "40",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Apparel",
    "IconUrl": "apparel-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "2",
    "FairCategoryUrl": "#",
    "DisplayOrder": "1",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "41",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Fabric",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "6",
    "FairCategoryUrl": "#",
    "DisplayOrder": "2",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "42",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Allied",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "17",
    "FairCategoryUrl": "#",
    "DisplayOrder": "4",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "57",
    "tradefairid": "55964",
    "ResourceId": "26",
    "ResourceImageDetailId": "21",
    "Name": "Exhibitors",
    "IconUrl": "/header/apparel.svg",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "1",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "58",
    "tradefairid": "55964",
    "ResourceId": "27",
    "ResourceImageDetailId": "22",
    "Name": "Corporate Videos",
    "IconUrl": "/header/fabric.svg",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "6",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "59",
    "tradefairid": "55964",
    "ResourceId": "28",
    "ResourceImageDetailId": "23",
    "Name": "Fair Lobby",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "60",
    "tradefairid": "55964",
    "ResourceId": "29",
    "ResourceImageDetailId": "46",
    "Name": "Auditorium",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "2",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "61",
    "tradefairid": "55964",
    "ResourceId": "32",
    "ResourceImageDetailId": "23",
    "Name": "Networking",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "4",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "62",
    "tradefairid": "55964",
    "ResourceId": "34",
    "ResourceImageDetailId": "26",
    "Name": "Knowledge",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "3",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "63",
    "tradefairid": "55964",
    "ResourceId": "35",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "7",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "64",
    "tradefairid": "55964",
    "ResourceId": "36",
    "Name": "About Organizor",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "8",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "65",
    "tradefairid": "55964",
    "ResourceId": "37",
    "Name": "About the Event",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "9",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "66",
    "tradefairid": "55964",
    "ResourceId": "41",
    "Name": "Footer Exhibitor Directory",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "67",
    "tradefairid": "55964",
    "ResourceId": "42",
    "Name": "F2F Services",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "10",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "68",
    "tradefairid": "55964",
    "ResourceId": "43",
    "Name": "Contact US",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "11",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "69",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Category List",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "0",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "IsMandatory": "0"
  },
  {
    "id": "70",
    "tradefairid": "55964",
    "ResourceId": "35",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "0",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "5",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "71",
    "tradefairid": "55964",
    "ResourceId": "49",
    "ResourceImageDetailId": "36",
    "Name": "Exhibitors",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "3",
    "IsMandatory": "0"
  },
  {
    "id": "72",
    "tradefairid": "55964",
    "ResourceId": "50",
    "ResourceImageDetailId": "35",
    "Name": "Auditorium",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "2",
    "IsMandatory": "0"
  },
  {
    "id": "73",
    "tradefairid": "55964",
    "ResourceId": "51",
    "ResourceImageDetailId": "34",
    "Name": "Knowledge",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "1",
    "IsMandatory": "0"
  },
  {
    "id": "74",
    "tradefairid": "55964",
    "ResourceId": "52",
    "ResourceImageDetailId": "37",
    "Name": "Networking",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "4",
    "IsMandatory": "0"
  },
  {
    "id": "75",
    "tradefairid": "55964",
    "ResourceId": "35",
    "ResourceImageDetailId": "38",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "5",
    "IsMandatory": "0"
  },
  {
    "id": "79",
    "tradefairid": "55964",
    "ResourceId": "56",
    "ResourceImageDetailId": "45",
    "Name": "Booth Detail",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMandatory": "0"
  },
  {
    "id": "95",
    "tradefairid": "55964",
    "ResourceId": "67",
    "Name": "Signup",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "96",
    "tradefairid": "55964",
    "ResourceId": "69",
    "Name": "Name",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "97",
    "tradefairid": "55964",
    "ResourceId": "70",
    "Name": "Designation",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "98",
    "tradefairid": "55964",
    "ResourceId": "71",
    "Name": "Company",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "99",
    "tradefairid": "55964",
    "ResourceId": "72",
    "Name": "Business Category",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "100",
    "tradefairid": "55964",
    "ResourceId": "73",
    "Name": "Email",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "101",
    "tradefairid": "55964",
    "ResourceId": "74",
    "Name": "Mobile",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "102",
    "tradefairid": "55964",
    "ResourceId": "75",
    "Name": "City",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "104",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "103",
    "IsMandatory": "0"
  },
  {
    "id": "105",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "104",
    "IsMandatory": "0"
  },
  {
    "id": "106",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "104",
    "IsMandatory": "0"
  },
  {
    "id": "107",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "105",
    "IsMandatory": "0"
  }
];

function connectChildren(root, arr) {
    root.children = [];

    // connect the children from the array to the root
    for (let child of arr) {
        if (child.ParentTradeFairResourceId == root.id) {
            root.children.push(child);
        }
    };

    // for each child, construct the tree in this same manner
    for (let child of root.children) {
        child = connectChildren(child, arr);
    }

    return root;
}

function main(arr) {
    // Get the list of all roots
    let roots = arr.filter(e => !e.ParentTradeFairResourceId);

    // Attach children to each root
    for (let root of roots) {
        root = connectChildren(root, arr);
    }
    console.log('Result object:', roots);
}

main(newArr);

希望本文能帮助您解决问题。假设您想为树保留最大深度,您可以传递一个深度,在每次递归调用时增加它,并且仅在深度等于最大值时才添加 children。这也将防止意外的无限递归。如果这不是您正在寻找的解决方案,您实际上应该创建一个更简单的示例:从输入数组中删除所有不必要的字段并明确说明您希望输出的内容。

var data = [{
    "id": "39",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Yarn",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "15",
    "FairCategoryUrl": "#",
    "DisplayOrder": "3",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "40",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Apparel",
    "IconUrl": "apparel-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "2",
    "FairCategoryUrl": "#",
    "DisplayOrder": "1",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "41",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Fabric",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "6",
    "FairCategoryUrl": "#",
    "DisplayOrder": "2",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "42",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Allied",
    "IconUrl": "yarn-icon.jpg",
    "IsForMobile": "0",
    "IsForDesktop": "0",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "57",
    "FairCategoryId": "17",
    "FairCategoryUrl": "#",
    "DisplayOrder": "4",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "57",
    "tradefairid": "55964",
    "ResourceId": "26",
    "ResourceImageDetailId": "21",
    "Name": "Exhibitors",
    "IconUrl": "/header/apparel.svg",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "1",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "58",
    "tradefairid": "55964",
    "ResourceId": "27",
    "ResourceImageDetailId": "22",
    "Name": "Corporate Videos",
    "IconUrl": "/header/fabric.svg",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "6",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "59",
    "tradefairid": "55964",
    "ResourceId": "28",
    "ResourceImageDetailId": "23",
    "Name": "Fair Lobby",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "60",
    "tradefairid": "55964",
    "ResourceId": "29",
    "ResourceImageDetailId": "46",
    "Name": "Auditorium",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "2",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "61",
    "tradefairid": "55964",
    "ResourceId": "32",
    "ResourceImageDetailId": "23",
    "Name": "Networking",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "4",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "62",
    "tradefairid": "55964",
    "ResourceId": "34",
    "ResourceImageDetailId": "26",
    "Name": "Knowledge",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "3",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "63",
    "tradefairid": "55964",
    "ResourceId": "35",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "7",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "64",
    "tradefairid": "55964",
    "ResourceId": "36",
    "Name": "About Organizor",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "8",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "65",
    "tradefairid": "55964",
    "ResourceId": "37",
    "Name": "About the Event",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "9",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "66",
    "tradefairid": "55964",
    "ResourceId": "41",
    "Name": "Footer Exhibitor Directory",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "67",
    "tradefairid": "55964",
    "ResourceId": "42",
    "Name": "F2F Services",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "10",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "68",
    "tradefairid": "55964",
    "ResourceId": "43",
    "Name": "Contact US",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "0",
    "DisplayOrder": "11",
    "GroupId": "3",
    "IsMandatory": "0"
  },
  {
    "id": "69",
    "tradefairid": "55964",
    "ResourceId": "46",
    "Name": "Category List",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "0",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "IsMandatory": "0"
  },
  {
    "id": "70",
    "tradefairid": "55964",
    "ResourceId": "35",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "0",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "1",
    "IsHeader": "1",
    "DisplayOrder": "5",
    "GroupId": "1",
    "IsMandatory": "0"
  },
  {
    "id": "71",
    "tradefairid": "55964",
    "ResourceId": "49",
    "ResourceImageDetailId": "36",
    "Name": "Exhibitors",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "3",
    "IsMandatory": "0"
  },
  {
    "id": "72",
    "tradefairid": "55964",
    "ResourceId": "50",
    "ResourceImageDetailId": "35",
    "Name": "Auditorium",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "2",
    "IsMandatory": "0"
  },
  {
    "id": "73",
    "tradefairid": "55964",
    "ResourceId": "51",
    "ResourceImageDetailId": "34",
    "Name": "Knowledge",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "1",
    "IsMandatory": "0"
  },
  {
    "id": "74",
    "tradefairid": "55964",
    "ResourceId": "52",
    "ResourceImageDetailId": "37",
    "Name": "Networking",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "4",
    "IsMandatory": "0"
  },
  {
    "id": "75",
    "tradefairid": "55964",
    "ResourceId": "35",
    "ResourceImageDetailId": "38",
    "Name": "Social Wall",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "59",
    "DisplayOrder": "5",
    "IsMandatory": "0"
  },
  {
    "id": "79",
    "tradefairid": "55964",
    "ResourceId": "56",
    "ResourceImageDetailId": "45",
    "Name": "Booth Detail",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMandatory": "0"
  },
  {
    "id": "95",
    "tradefairid": "55964",
    "ResourceId": "67",
    "Name": "Signup",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "IsMandatory": "0"
  },
  {
    "id": "96",
    "tradefairid": "55964",
    "ResourceId": "69",
    "Name": "Name",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "97",
    "tradefairid": "55964",
    "ResourceId": "70",
    "Name": "Designation",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "98",
    "tradefairid": "55964",
    "ResourceId": "71",
    "Name": "Company",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "99",
    "tradefairid": "55964",
    "ResourceId": "72",
    "Name": "Business Category",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "100",
    "tradefairid": "55964",
    "ResourceId": "73",
    "Name": "Email",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "101",
    "tradefairid": "55964",
    "ResourceId": "74",
    "Name": "Mobile",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "1"
  },
  {
    "id": "102",
    "tradefairid": "55964",
    "ResourceId": "75",
    "Name": "City",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "103",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "Country",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "0",
    "IsActive": "1",
    "IsDesktopDefault": "0",
    "IsMobileDefault": "0",
    "IsMenu": "0",
    "IsHeader": "0",
    "ParentTradeFairResourceId": "95",
    "IsMandatory": "0"
  },
  {
    "id": "104",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "103",
    "IsMandatory": "0"
  },
  {
    "id": "105",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "104",
    "IsMandatory": "0"
  },
  {
    "id": "106",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "104",
    "IsMandatory": "0"
  },
  {
    "id": "107",
    "tradefairid": "55964",
    "ResourceId": "76",
    "Name": "state",
    "IsForMobile": "1",
    "IsForDesktop": "1",
    "IsWithLogin": "1",
    "IsActive": "1",
    "IsDesktopDefault": "1",
    "IsMobileDefault": "1",
    "IsMenu": "1",
    "IsHeader": "1",
    "ParentTradeFairResourceId": "105",
    "IsMandatory": "0"
  }
]
const tree = function(data, root) {
  var t = {};
  data.forEach(o => {
    Object.assign(t[o.id] = t[o.id] || {}, o);
    t[o.ParentTradeFairResourceId] = t[o.ParentTradeFairResourceId] || {};
    t[o.ParentTradeFairResourceId].children = t[o.ParentTradeFairResourceId].children || [];
    t[o.ParentTradeFairResourceId].children.push(t[o.id]);
  });
  return t.undefined.children;
}(data, null);
console.log('Result object:', tree);

我按照这个逻辑解决了这个问题。