Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript Object Structure not pushing data into array over existing key

I have array name as DATA it contains list of objects of user ,some users have more than one roles , I need to convert the key role to a array and push the other roles of that user into that array , I have written this code but don’t knnow why its not working

try {
  let results = await query(`
  select
  u.UserId,
  u.UserName,
  u.UserStatus,
  u.MobileNumber,
  u.Email,
  cm.ClientName,
  r.RoleId,
  r.RoleName,
  sm.SystemName
from
  \`User\` u
  join UserRoleMapping urm on urm.UserId=u.UserId
  join Role r on r.RoleId=urm.RoleId
  join SystemMaster sm on sm.MasterSystemId=urm.MasterSystemId
  join Client_Master cm on cm.ClientMasterId=u.ClientMasterId`);

  if (!results) {
    return success(res, "No data Found", []);
  } else {
    let arr = [];
    let obj = {};
    for (let i of results) {
      let user_id = i["UserId"];
      let obj1 = {};
      if (obj.hasOwnProperty(i["UserId"])) {
        obj1["Role"] = [];
        obj1["Role"].push(i["RoleName"]);
        obj1["System"] = [];
        obj1["System"].push(i["SystemName"]);
        console.log("inside if")
      } else{
        obj1["UserName"] = i["UserName"];
        obj1["UserId"] = user_id;
        obj1["Mobile"] = i["MobileNumber"];
        obj1["Email"] = i["Email"];
        obj1["UserStatus"] = i["UserStatus"];
        obj1["Role"] = [];
        obj1["Role"].push(i["RoleName"]);
        obj1["System"] = [];
        obj1["System"].push(i["SystemName"]);
        obj[user_id] = obj1;
      }
    }
    arr.push(obj);

    let data = arr.map((user) => {
      let keys = Object.keys(user);
      let arr1 = [];
      for (i of keys) {
        arr1.push(user[i]);
      }
      return arr1;
    });

    return success(res, "Users fetched successfully", data[0]);
  }

Please help solve this

Current Output is this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
"SUCCESS": true,
"MESSAGE": "Users fetched successfully",
"DATA": [
    {
        "UserId": 16,
        "UserName": "Karan Fulare",
        "UserStatus": 1,
        "MobileNumber": "9359661324",
        "Email": "karan.fulare@greentiger.in",
        "ClientName": "GT",
        "RoleId": 1,
        "RoleName": "Admin",
        "SystemName": "Sales"        },
    {
        "UserId": 15,
        "UserName": "Venkateswarlu",
        "UserStatus": 1,
        "MobileNumber": "7893356594",
        "Email": "venki@greentiger.in",
        "ClientName": "GT",
        "RoleId": 10,
        "RoleName": "TeleCaller",
        "SystemName": "Sales"        },
   
    {
        "UserId": 16,
        "UserName": "Karan Fulare",
        "UserStatus": 1,
        "MobileNumber": "9359661324",
        "Email": "karan.fulare@greentiger.in",
        "ClientName": "GT",
        "RoleId": 2,
        "RoleName": "HRC Manager",
        "SystemName": "Admin"        }
]

}

And Expected Output is this

    {
        "UserId": 16,
        "UserName": "Karan Fulare",
        "UserStatus": 1,
        "MobileNumber": "9359661324",
        "Email": "karan.fulare@greentiger.in",
        "ClientName": "GT",
        "RoleId": 2,
        "RoleName": ["HRC Manager","Telecaller"]
        "SystemName": ["Admin","sales"]     

}

>Solution :

It seems like the code is not updating the Role and System arrays correctly for the users who have multiple roles. You need to check if the user already exists in the obj object and if so, you need to update the Role and System arrays instead of creating a new object.

try {
  let results = await query(`
  select
  u.UserId,
  u.UserName,
  u.UserStatus,
  u.MobileNumber,
  u.Email,
  cm.ClientName,
  r.RoleId,
  r.RoleName,
  sm.SystemName
from
  \`User\` u
  join UserRoleMapping urm on urm.UserId=u.UserId
  join Role r on r.RoleId=urm.RoleId
  join SystemMaster sm on sm.MasterSystemId=urm.MasterSystemId
  join Client_Master cm on cm.ClientMasterId=u.ClientMasterId`);

  if (!results) {
    return success(res, "No data Found", []);
  } else {
    let obj = {};
    for (let i of results) {
      let user_id = i["UserId"];
      if (obj.hasOwnProperty(user_id)) {
        obj[user_id]["Role"].push(i["RoleName"]);
        obj[user_id]["System"].push(i["SystemName"]);
      } else {
        obj[user_id] = {
          "UserName": i["UserName"],
          "UserId": user_id,
          "Mobile": i["MobileNumber"],
          "Email": i["Email"],
          "UserStatus": i["UserStatus"],
          "Role": [i["RoleName"]],
          "System": [i["SystemName"]]
        };
      }
    }

    let data = Object.values(obj);

    return success(res, "Users fetched successfully", data);
  }
} catch (error) {
  console.error(error);
  return errorResponse(res, error.message);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading