0
var roles = {
    Guest: ["CAN_REGISTER"],
    Player: ["CAN_LOGIN", "CAN_CHAT"],
    Admin: ["CAN_KICK", "CAN_LOGIN", "CAN_CHAT"]
};

that is my object and I am trying to check if the user has specific perms

get_perms: function(player, perm) {
    let arrayLength = accounts.length;
    let name = player.name;

    if (arrayLength == 0 && (perm == "CAN_CHAT" || perm == "CAN_LOGIN" || perm == "CAN_KICK")){
        return false;
    }

    for (var i = 0; i < arrayLength; i++)
    {
        if (accounts[i][0] == name)
        {
            for (var key in roles)
            {
                if (roles.hasOwnProperty(key))
                {
                    if (accounts[i][2] == key){

                        if (roles[key] == perm){
                            for (var x = 0; x < roles[key].length; x++){
                                if (roles[key][x] == perm){
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
        }
        else{
            return false;
        }
    }
}

account[i][2] is the role of the player which matches the name, I am trying to check if that role has perm which is sent to the function, for example "CHAT_PERMS"

ClarMarko
  • 13
  • 1
  • 1
    Possible duplicate of [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – Striped Mar 03 '18 at 02:01
  • @Striped as you can see, I did what is being said in that topic and I didn't manage to do what I wanted, read my topic, basically, I want to check if a specific KEY has a specific PERM, which I failed to – ClarMarko Mar 03 '18 at 02:04
  • Did you try `indexOf` at all? Also its pretty obvious your code wont work, you check `roles[key] == perm` and `roles[key][x] == perm` they won't both be true – Nick is tired Mar 03 '18 at 02:05

2 Answers2

1
const roles = {
    Guest: ["CAN_REGISTER"],
    Player: ["CAN_LOGIN", "CAN_CHAT"],
    Admin: ["CAN_KICK", "CAN_LOGIN", "CAN_CHAT"]
};

const arr = Object.keys(roles).reduce((acc, cur) => [...acc, ...roles[cur]], []);
console.log(arr);
const rightSet = new Set(arr);
console.log(rightSet.has("CAN_CHAT"))

This is my solution using ES6:

  1. Use reduce with array spread syntax to create a flat array, which give you [ "CAN_REGISTER", "CAN_LOGIN", "CAN_CHAT", "CAN_KICK", "CAN_LOGIN", "CAN_CHAT" ]
  2. Save this array in a set
  3. Use has() function to check if the right in the Set
FisNaN
  • 2,517
  • 2
  • 24
  • 39
0

Use a for-loop and the function includes to check the role and permission.

var roles = {
    Guest: ["CAN_REGISTER"],
    Player: ["CAN_LOGIN", "CAN_CHAT"],
    Admin: ["CAN_KICK", "CAN_LOGIN", "CAN_CHAT"]
};

var playerRoles = ['Admin', 'Guest'];
var perm = 'CAN_LOGIN';

var found = false;
for (var role of playerRoles) {
  if ((found = roles[role].includes(perm))) {
    console.log("Permission '"+perm+"' found in Role '" + role + "'");
    break;
  }
}

if (!found) {
    console.log("Permission '"+perm+"' didn't found");
}
Ele
  • 33,468
  • 7
  • 37
  • 75