从 JSON 对象获取父 ID/名称列表
P粉190443691
P粉190443691 2023-09-07 16:38:02
0
1
434

我有一个包含文件夹结构详细信息的 JSON 数据。 所需要的是给出文件夹名称并检索其父层次结构。

我的结构如下...

FolderHome lvl1 lvl11 lvl12 lvl13 lvl2 lvl21 lvl22 lvl23

我需要的是输出

  • FolderHome\lvl2如果findparent('lvl23')被调用。
  • FolderHome\lvl1如果findparent('lvl12')被调用。
{ "Items": [ { "Name": "FolderHome", "ID": 1, "ParentID": 0, "OwnerID": 0, "HasParent": false, "HasOwner": false, "ValueListID": 4, "DisplayID": "1", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{1F58043A-2876-44A5-B3FA-B170098C32F1}" }, { "Name": "lvl1", "ID": 11, "ParentID": 1, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "11", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{915BA6C4-3FDE-4B8F-AD87-A0054C8A6A9C}" }, { "Name": "lvl2", "ID": 15, "ParentID": 1, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "15", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{84341658-816A-4987-87F6-75CAE1AE81E7}" }, { "Name": "lvl21", "ID": 16, "ParentID": 15, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "16", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{0AFC4C26-D54B-4959-9276-D3ED1B447316}" }, { "Name": "lvl22", "ID": 17, "ParentID": 15, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "17", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{6005594F-71E3-4880-8843-36081DB7CA29}" }, { "Name": "lvl23", "ID": 18, "ParentID": 15, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "18", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{0B2453DA-8ED0-4C6C-8F3B-F05DD3B63AA1}" }, { "Name": "lvl11", "ID": 12, "ParentID": 11, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "12", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{4014C72D-FC10-4B21-AD62-6B7562F44833}" }, { "Name": "lvl12", "ID": 13, "ParentID": 11, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "13", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{9351B350-DFAC-4BE2-ABF0-AD5FCAC7F48A}" }, { "Name": "lvl13", "ID": 14, "ParentID": 11, "OwnerID": 0, "HasParent": true, "HasOwner": false, "ValueListID": 4, "DisplayID": "14", "HasIcon": false, "HasAutomaticPermission": false, "IsDeleted": false, "ItemGUID": "{661480EA-7382-4023-9FF3-0C7205DAB5BF}" } ] }
// If the required node is found, return true // matching nodes added to the result parameter var findParent = function (parentnode, id, result) { if (parentnode.Id == id){ result.push(parentnode.Parent); return true; } if (parentnode.children) { for (var i = 0; i < parentnode.children.length; i++) { var node = parentnode.children[i]; if (parentnode.Parent && findParent(node, id, result)) { result.push(parentnode.Parent); return true; } } } // no matches found - return false return false; } var result = []; findParent(obj, "13", result) console.log(result);

我的代码只是返回[]

P粉190443691
P粉190443691

全部回复 (1)
P粉404539732

您可以采用两个辅助对象,一个用于姓名,另一个用于父母。

const items = [{ Name: "FolderHome", ID: 1, ParentID: 0 }, { Name: "lvl1", ID: 11, ParentID: 1 }, { Name: "lvl2", ID: 15, ParentID: 1 }, { Name: "lvl21", ID: 16, ParentID: 15 }, { Name: "lvl22", ID: 17, ParentID: 15 }, { Name: "lvl23", ID: 18, ParentID: 15 }, { Name: "lvl11", ID: 12, ParentID: 11 }, { Name: "lvl12", ID: 13, ParentID: 11 }, { Name: "lvl13", ID: 14, ParentID: 11 }], idName = Object.fromEntries(items.map(({ ID, Name }) => [ID, Name])), nameParentID = Object.fromEntries(items.map(({ Name, ParentID }) => [Name, ParentID])), getParents = name => { const p = nameParentID[name]; if (!p) return ''; const pName = getParents(idName[p]); return pName + (pName && '\') + idName[p]; } console.log(getParents('lvl23')); // FolderHome\lvl2 console.log(getParents('lvl12')); // FolderHome\lvl1
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责声明 Sitemap
    PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!