mhery
mhery

Reputation: 2248

Iterate over object inside object with javascript that have regex

I'm creating some descriptions with Regex using the name of the privileges I have, that are these:

var privileges = [
    "APP_SOMEONE_SRV",
    "APP_ANYONE_SRV",
    "ADM_AD_XX"
];

var descriptions = {
    "NETWORK": {
        "GROUP": {
            "^APP_(.*)$": {
                "^APP_" : "PATH",
                "^APP_A05_(.*)" : "$1",
                "_SRV" : "ON SERVER"
            },
            "^A05_(.*)" : "Group $1 at AD",
            "^ADM_AD_(.*)": "Administrator $1 at AD",       
        }
    }
}

I'm debugging the code with Chrome's Developer Tool, printing some vars with window.console.log:

privileges.forEach(function(role) {
    var base = "NETWORK";
    var type = "GROUP";
    Object.keys(descriptions).forEach(function(baseReg) {
        if (base.match(new RegExp(baseReg))) {
            Object.keys(descriptions[baseReg]).forEach(function(typeReg) {
                if (type.match(new RegExp(typeReg))) {
                    Object.keys(descriptions[baseReg][typeReg]).forEach(function(roleReg) {

                    var reg = RegExp(roleReg);
                    window.console.log("role: " + role);
                    window.console.log("roleReg: " + typeof roleReg + " >> " + roleReg);
                    if (role.match(reg)) {
                        var suggestDescription = role.replace(reg, descriptions[baseReg][typeReg][roleReg]);
                        window.console.log("description: " + suggestDescription);    
                    }
                    if (roleReg == Object.prototype.toString.call(roleReg) === '[object Object]'){
                        window.console.log(roleReg + " is object");
                    }
                    window.console.log("");


                    });
                }
            });
        }
    });
});

On console, I get that "^APP_(.*)$" is always a string, when it should be an object:

VM107:31 role: APP_SOMEONE_SRV
VM107:32 roleReg: string >> ^APP_(.*)$
VM107:35 description: [object Object]
VM107:40 
VM107:31 role: APP_SOMEONE_SRV
VM107:32 roleReg: string >> ^A05_(.*)
VM107:40 
VM107:31 role: APP_SOMEONE_SRV
VM107:32 roleReg: string >> ^ADM_AD_(.*)
VM107:40 
VM107:31 role: APP_ANYONE_SRV
VM107:32 roleReg: string >> ^APP_(.*)$
VM107:35 description: [object Object]
VM107:40 
VM107:31 role: APP_ANYONE_SRV
VM107:32 roleReg: string >> ^A05_(.*)
VM107:40 
VM107:31 role: APP_ANYONE_SRV
VM107:32 roleReg: string >> ^ADM_AD_(.*)
VM107:40 
VM107:31 role: ADM_AD_XX
VM107:32 roleReg: string >> ^APP_(.*)$
VM107:40 
VM107:31 role: ADM_AD_XX
VM107:32 roleReg: string >> ^A05_(.*)
VM107:40 
VM107:31 role: ADM_AD_XX
VM107:32 roleReg: string >> ^ADM_AD_(.*)
VM107:35 description: Administrator XX at AD

I already tried to change the comparison to check that if the actual roleReg is a object, but it didn't change anything. Does it have other way to iterate over the descriptions, checking before that if the actual one is a object? Because, for example, "^A05_(.*)" is not a object

Upvotes: 0

Views: 233

Answers (1)

James Wilkins
James Wilkins

Reputation: 7377

roleReg is the property value (string), which is why. reg is the object you are making with that value, and that is an object. ;)

To clarify Object.keys(descriptions[baseReg][typeReg]) is iterating over descriptions['NETWORK']['GROUP'] and that will give you the property NAMEs (strings), not the value of the property. var reg = RegExp(roleReg); creates a regex with the property NAME, which is ^APP_(.*)$.

descriptions[baseReg][typeReg][roleReg] will give the you property object you are looking for.

Try this:

privileges.forEach(function(role) {
    var base = "NETWORK";
    var type = "GROUP";
    Object.keys(descriptions).forEach(function(baseReg) {
        if (base.match(new RegExp(baseReg))) {
            Object.keys(descriptions[baseReg]).forEach(function(typeReg) {
                if (type.match(new RegExp(typeReg))) {
                    Object.keys(descriptions[baseReg][typeReg]).forEach(function(roleReg) {
                    var reg = RegExp(roleReg);
                    var roleRegValue = descriptions[baseReg][typeReg][roleReg];
                    window.console.log("role: " + role);
                    window.console.log("roleReg Property: " + typeof roleReg + " >> " + roleReg);
                    window.console.log("roleReg Value: " + typeof roleRegValue + " >> " + roleRegValue);
                    if (role.match(reg)) {
                        var suggestDescription = role.replace(reg, descriptions[baseReg][typeReg][roleReg]);
                        window.console.log("description: " + suggestDescription);    
                    }
                    if (roleRegValue == Object.prototype.toString.call(roleRegValue) === '[object Object]'){
                        window.console.log(roleRegValue + " is object");
                    }
                    window.console.log("");


                    });
                }
            });
        }
    });
});

Upvotes: 1

Related Questions