简体   繁体   中英

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.

  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.

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?

 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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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