搜尋

首頁  >  問答  >  主體

從 JSON 物件取得父 ID/名稱列表

我有一個包含資料夾結構詳細資訊的 JSON 資料。 所需要的是給出資料夾名稱並檢索其父層次結構。

我的結構如下...

FolderHome
   lvl1
     lvl11
     lvl12
     lvl13
   lvl2
     lvl21
     lvl22
     lvl23

我需要的是輸出

{
    "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粉190443691P粉190443691442 天前496

全部回覆(1)我來回復

  • P粉404539732

    P粉4045397322023-09-08 12:29:46

    您可以採用兩個輔助對象,一個用於姓名,另一個用於父母。

    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

    回覆
    0
  • 取消回覆