0

I have following code snippet. I need to add this to a loop. What is the best way that I can follow.

A js object named Data is passed to the following JavaScript method and I need to check whether certain key are assigned the value "". Currently I'm using set of 'if' statement as below but I would like to acheive this using a loop.

privateMethods.generatePasswordDataPayload = function (Data) {
        if (Data["passwordLength"] == "") {
            Data["passwordLength"] = null;
        }
        if (Data["passwordComplexCharactors"] == "") {
            Data["passwordComplexCharactors"] = null;
        }
        if (Data["passwordExpTime"] == "") {
            Data["passwordExpTime"] = null;
        }
        if (Data["passwordHistory"] == "") {
            Data["passwordHistory"] = null;
        }
        if (Data["passwordAttempts"] == "") {
            Data["passwordAttempts"] = null;
        }
 }

Can I use any regex patterns and do something similar as below (pseudo code)

var i = 0;
while (Data.length >= i){
  if ((Data["password(regex pattern check)") == ""){
   (Data["password(regex pattern check)") == null;
  }
 i++;
}

Please note that I'm using JavaScript here.

Mad
  • 435
  • 2
  • 17
  • 1
    Please show us what is inside `Array` – Weedoze Feb 08 '17 at 12:29
  • sorry, it is not an array but a JavaScript object – Mad Feb 08 '17 at 12:41
  • But regex can check only specified strings or a particular expression of desired set of rules which would be the combination of some characters. But in your case how would you determine at what position character is capital or not this may be the issue. But afterall if you would use an array of specified strings to match may increase the time complexity of your program and would not be efficient. – sagar Feb 08 '17 at 12:43
  • @Mad Show us the full `Data` object – Weedoze Feb 08 '17 at 12:44
  • All of my key name are starting from the name "password...". So it has the pattern "password...". After "password" there can be any name which doesn't matter. So what I need to check is, if any key starting from the name "password..." has set with a value "". – Mad Feb 08 '17 at 12:48
  • @Mad if all your keys names start with "*password*" - Why do you even need to check the name ? Just iterates the values... – Weedoze Feb 08 '17 at 12:49
  • @Weedoze Actually user entered information, capturing form a Web UI, is set as input data for the Data. I'm working with JavaScript. – Mad Feb 08 '17 at 12:56
  • @Weedoze, There are some other keys which are not starting with "_password_" – Mad Feb 08 '17 at 12:57

2 Answers2

1

If you want to list each property and set a value you can do something like this

for(objectName in Data) { 
     if(Data[objectName]=="") {
          Data[objectName] == null;
    }
}
Community
  • 1
  • 1
Pred05
  • 492
  • 1
  • 3
  • 13
  • Thank you, but my problem I have set of keys starting from the name "password....". Like "passwordLength", "passwordHistory", "passwordAttempts" etc. I need to check whether there is a "" value set for any of these kays. If so I need to replace it with the value "null" instead of "". – Mad Feb 08 '17 at 12:44
  • Ok, so you can add an if which do something like objectName.indexOf("password") == 0 – Pred05 Feb 08 '17 at 12:49
0

String.prototype.indexOf will return the index of the first occurence of the string passed as parameter. So to get all the keys that start with "password", you have to check if indexOf("password") return 0 or not (if it returned 0 then the key start with password, otherwise it doesn't).

privateMethods.generatePasswordDataPayload = function (Data) {
    for(var key in Data) { // for each key in Data
        if(key.indexOf("password") == 0) { // if the key starts with "password"
            Data[key] = null; // set the value to null
        }
    }
 }
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73