简体   繁体   中英

JavaScript - How to loop through array of objects

I have been working on keycloak admin rest api method and I got most of the method working. I am able to create users and create roles using excel sheet read in nodejs and that works well. I am able to list all user and list all roles created. Now what i want to do is check if the role exist in the excel file using comma separated and then assign that users only the roles method in the excel file

I will share my code

function read user and role

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;
}

Output

在此处输入图片说明

here I want to check the employee excel file and according the role in the column i want to assign to each user each role but it does not work it just assign four roles to first four users

在此处输入图片说明

Function Call

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) => {
                    })
                }
            })
        })

output - the role for each user not the way the excel file is specified

![在此处输入图片说明

From the Keycloak response, besides the user ID , you will also need its username , why? Because with that username you can match the username retrieved from Excel.

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

Now you have the user's IDs and usernames , which will be needed to create the roles on Keycloak and match the Excel content, respectively.

Then you must get the username from the array usernames , search on the userRoleTobCreated for that username , and get its roles . Then, you will add those roles to the user. For that, first you will have to use the role name extracted from Excel, and get its corresponding index on your roleName array. You will use that index to get the role ID from the array roleId . The code would be something like:

// 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)...
    }
};

Now bear in mind that, I do not know about JavaScript, Node.js, and so on. So this pseudo-code might need some adaptions, probably in terms of better idioms and performance. Nevertheless, the algorithm is there.

I would also rename some of your variables userid , roleId , and roleName to userIDs , roleIDs , and roleNames , respectively. The original names make the code slightly hard to follow, and are also misleading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM