簡體   English   中英

JavaScript - 如何遍歷對象數組

[英]JavaScript - How to loop through array of objects

我一直在研究 keycloak admin rest api 方法,並且大部分方法都在工作。 我能夠使用在 nodejs 中讀取的 excel 表來創建用戶和創建角色,並且效果很好。 我能夠列出所有用戶並列出所有創建的角色。 現在我想要做的是使用逗號分隔檢查 excel 文件中是否存在該角色,然后僅將 excel 文件中的角色方法分配給該用戶

我會分享我的代碼

函數讀取用戶和角色

let userRoleTobCreated = ReadFromExcel('./uploads/employees-roles.xlsx');
function ReadFromExcel() {
    let userRoleTobCreated = []
    xlsxFile(filename).then((rows) => {
        rows.forEach(row => {
            let userObject = {
                username: row[0],
                lastName: row[1],
                firstName: row[2],
                email: row[3],
                roles: row[4].split(",")
            }
            userRoleTobCreated.push(userObject);
        });
    })
    return userRoleTobCreated;
}

輸出

在此處輸入圖片說明

在這里,我想檢查員工 excel 文件,並根據列中的角色,我想為每個用戶分配每個角色,但它不起作用,只是將四個角色分配給前四個用戶

在此處輸入圖片說明

函數調用

GetUser(userid, kc_accessToken).then((resp) => {
 userid = resp.data.map(userRoleTobCreated => userRoleTobCreated.id);

  GetRole(roleId, roleName, kc_accessToken).then((resp) => {
   roleId = resp.data.map(userRoleTobCreated => userRoleTobCreated.id);
   roleName = resp.data.map(userRoleTobCreated => userRoleTobCreated.name);

// Call Api to loop through each customer and assign each role to each customer
// this method just loops through the four role and assign it to the first four users only

      for (var i = 0; i < userid.length; i++) {
      AssignRole(userid[i], roleId[i], roleName[i], kc_clientUUID, kc_accessToken).then((resp) => {
                    })
                }
            })
        })

輸出 - 每個用戶的角色而不是指定 excel 文件的方式

![在此處輸入圖片說明

從 Keycloak 響應中,除了user ID之外,您還需要它的username ,為什么? 因為使用該username您可以匹配從 Excel 檢索到的username

GetUser(userid, kc_accessToken).then((resp) => {
 ...
 usernames = resp.data.map(userRoleTobCreated => userRoleTobCreated.username);

現在您有了用戶的IDsusernames ,這將分別用於在 Keycloak 上創建角色和匹配 Excel 內容。

然后,您必須從數組usernames獲取username usernames ,在userRoleTobCreated上搜索該username ,並獲取其roles 然后,您將這些角色添加到用戶。 為此,首先您必須使用從 Excel 中提取的role name ,並在您的roleName數組中獲取其對應的索引。 您將使用該索引從數組roleId獲取role ID 代碼類似於:

// For each userID retrieved from Keycloak
for (var i = 0; i < userid.length; i++) {
    roles = []
    // Find the username and get its roles
    for (var j = 0; j < userRoleTobCreated.length; j++)
        if(usernames[i] == userRoleTobCreated[j].username){
           roles = userRoleTobCreated[j].roles
           break;  
        }

    // add each one of the roles
    for(var r = 0; r < roles.length; r++){
       role_pos = roleName.indexOf(role[r])
       role_id = roleId[role_pos]
       AssignRole(userid[i], role_id, role[r], kc_clientUUID, kc_accessToken)...
    }
};

現在請記住,我不了解 JavaScript、Node.js 等。 所以這個偽代碼可能需要一些修改,可能是在更好的習慣用法和性能方面。 盡管如此,算法還是存在的。

我還要重命名你的一些變量的useridroleIdroleName ,以userIDsroleIDsroleNames ,分別。 原始名稱使代碼有點難以理解,並且也具有誤導性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM