Saya mempunyai data JSON yang mengandungi butiran struktur folder. Apa yang diperlukan ialah memberikan nama folder dan mendapatkan semula hierarki induknya.
Struktur saya adalah seperti berikut...
FolderHome lvl1 lvl11 lvl12 lvl13 lvl2 lvl21 lvl22 lvl23
Apa yang saya perlukan ialah output
FolderHomelvl2
如果 findparent('lvl23')
dipanggil. FolderHomelvl1
如果 findparent('lvl12')
dipanggil. { "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);
Kod saya baru sahaja kembali []
.
Anda boleh mengambil dua objek pembantu, satu untuk nama dan satu lagi untuk ibu bapa.