简体   繁体   English

将平面JSON转换为父子关系结构

[英]Converting Flat JSON To Parent-Child relational Structure

we want to convert Below JSON to Parent-Child Relation for a tree view structure, based on parent Link and Display Order. 我们想根据父链接和显示顺序将树下结构将JSON下面转换为父子关系。

  1. if parent link is empty it is parent 如果父链接为空,则为父
  2. if parent link exist it should add as a child 如果存在父链接,则应将其添加为子链接
  3. items should sort based on display order 项目应根据显示顺序排序
[{
  "ParentLink":{ },
  "Id":1,
  "Title":"Home ITSD test new",
  "Url":{
     "Description":"#",
     "Url":"https://technologies.sharepoint.com/"
  },
  "DisplayOrder":1,
  "IsActive":true,
  "ID":1},{
  "ParentLink":{

  },
  "Id":2,
  "Title":"Link6",
  "Url":{
     "Description":"#",
     "Url":"https://technologies.sharepoint.com/"
  },
  "DisplayOrder":2,
  "IsActive":true,
  "ID":2},{"ParentLink":{
     "Title":"Link6"
  },
  "Id":3,
  "Title":"link7",
  "Url":{
     "Description":"#",
     "Url":"https://technologies.sharepoint.com/"
  },
  "DisplayOrder":21,
  "IsActive":true,
  "ID":3},{
  "ParentLink":{
     "Title":"Link6"
  },"Id":4,
  "Title":"link8",
  "Url":{
     "Description":"#",
     "Url":"https://technologies.sharepoint.com/"
  },
  "DisplayOrder":22,
  "IsActive":true,
  "ID":4},{
  "ParentLink":{
     "Title":"link8"
  },
  "Id":5,
  "Title":"link9",
  "Url":{
     "Description":"#",
     "Url":"https://technologies.sharepoint.com/"
  },
  "DisplayOrder":221,
  "IsActive":true,
  "ID":5}]

is there any other libraries already available or any easy way to do so 是否有其他可用的库,或者是否有任何简单的方法

While Title and ParentLink.Title do not have matching values, you could take both values as lower case letters and use both to generate a tree by iterating the given array and use an object for assigning nodes and children. 虽然TitleParentLink.Title没有匹配的值,但是您可以将这两个值都转换为小写字母,并通过迭代给定的数组使用这两个值生成树,并使用一个对象来分配节点和子级。

For the wanted order, I suggest to sort the data in advance and take the nodes as the actual order for generating the tree. 对于所需的顺序,我建议事先对数据进行排序,并以节点为生成树的实际顺序。 This is easier than to sort later only parts. 这比仅对以后的部分进行排序要容易。

Just wondering, why has the data two id properties? 只是想知道,为什么数据具有两个id属性?

 var data = [{ ParentLink: {}, Id: 1, Title: "Home ITSD test new", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 1, IsActive: true, ID: 1 }, { ParentLink: {}, Id: 2, Title: "Link6", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 2, IsActive: true, ID: 2 }, { ParentLink: { Title: "Link6" }, Id: 3, Title: "link7", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 21, IsActive: true, ID: 3 }, { ParentLink: { Title: "Link6" }, Id: 4, Title: "link8", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 22, IsActive: true, ID: 4 }, { ParentLink: { Title: "link8" }, Id: 5, Title: "link9", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 221, IsActive: true, ID: 5 }], tree = function (data, root) { var r = [], o = {}; data.forEach(function (a) { var id = a.Title.toLowerCase(), parent = a.ParentLink && a.ParentLink.Title && a.ParentLink.Title.toLowerCase(); a.children = o[id] && o[id].children; o[id] = a; if (parent === root) { r.push(a); } else { o[parent] = o[parent] || {}; o[parent].children = o[parent].children || []; o[parent].children.push(a); } }); return r; }(data, undefined); console.log(tree); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM