Rufen Sie eine Liste der übergeordneten IDs/Namen von einem JSON-Objekt ab
P粉190443691
P粉190443691 2023-09-07 16:38:02
0
1
428

Ich habe JSON-Daten mit Details zur Ordnerstruktur. Es ist lediglich erforderlich, einen Ordnernamen anzugeben und die übergeordnete Hierarchie abzurufen.

Mein Aufbau ist wie folgt...

FolderHome lvl1 lvl11 lvl12 lvl13 lvl2 lvl21 lvl22 lvl23

Was ich brauche, ist Ausgabe

  • FolderHomelvl2如果findparent('lvl23')heißt.
  • FolderHomelvl1如果findparent('lvl12')heißt.
{ "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);

Mein Code kehrt gerade zurück[].

P粉190443691
P粉190443691

Antworte allen (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
    Neueste Downloads
    Mehr>
    Web-Effekte
    Quellcode der Website
    Website-Materialien
    Frontend-Vorlage
    Über uns Haftungsausschluss Sitemap
    Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!