Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Loop thru an array

Avatar

Level 3

Hello,

There are two text fields (employee and team). I want team to autopopulate based on employee using an array (I think this is the best way).

textfieldA has 1 email in it (of 60 possible). I want textfieldB to autopopulate with another email (of 5 possible) based on the rawValue of textfieldA.

My code is on a change event and looks like this (and does not work):


var x;

var 3dClos = new Array("first.last@domain.com","firs.las@domain.com","fir.la@domain.com");

if(email.rawValue = 3dClos[x]) {

    supemail.rawValue = "correct.email@domain.com";
    }
    else
    {
    supemail.rawValue = "";

I do not want to make a giant if else statement as I have in the past - this is the perfect opportunity to learn the smart way to do it (even if it is not using an array). So in closing - check an email, see if it belongs to a tema and display that team email in another field.

Can anybody help?

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi,

1) if(email.rawValue = 3dClos[x])

      you are making assignment instead of equalization, replace it with

     if(email.rawValue == 3dClos[x])

2) var x; is not initialized ithin you have to make loop like this:

var 3dClos = new Array("first.last@domain.com","firs.las@domain.com","fir.la@domain.com");

for (var i=0; i<3dClos.length;i++  ){

if(email.rawValue == 3dClos[i]) {

    supemail.rawValue = "correct.email@domain.com";
    }
    else
     {
      supemail.rawValue = "";

     }

}

BR,

Paul Butenko

View solution in original post

5 Replies

Avatar

Correct answer by
Level 6

Hi,

1) if(email.rawValue = 3dClos[x])

      you are making assignment instead of equalization, replace it with

     if(email.rawValue == 3dClos[x])

2) var x; is not initialized ithin you have to make loop like this:

var 3dClos = new Array("first.last@domain.com","firs.las@domain.com","fir.la@domain.com");

for (var i=0; i<3dClos.length;i++  ){

if(email.rawValue == 3dClos[i]) {

    supemail.rawValue = "correct.email@domain.com";
    }
    else
     {
      supemail.rawValue = "";

     }

}

BR,

Paul Butenko

Avatar

Level 3

Paul,

Thanks for the help. The array initially still did not work but only because I was using a number in my variabal (3dClos).

Once I changed that it worked fine.

Avatar

Level 3

Thanks for all the help. One more questions (at least about this topic!):

If I wanted to check a total of 6 arrays for matches how would I structure the rest of the code?

for (var i=0; i<Carlos.length;i++  ){

if(email.rawValue == Carlos[i]) {

    supemail.rawValue = "first.last@domain.com";
    }

    //does not equal Carlos so check the next array

        else if(email.rawValue != Carlos[i]) {
    {
         for (var i=0; i<Corey.length;i++  ){


          if(email.rawValue == Corey[i]) {


              supemail.rawValue = "last.first@domain.com";

    }

                    //does not equal Corey so check the next array

                       else if(email.rawValue != Ryan[i]) {
                   {
                        for (var i=0; i<Ryan.length;i++  ){


                         if(email.rawValue == Ryan[i]) {


                             supemail.rawValue = "everyone@domain.com";

}

Avatar

Level 6

Hi,

Your approach correct but it is hard to read. Maybe with one more variable it will more understandable:

var found = false;

for

(var i=0; i<Carlos.length;i++ ){

     if(email.rawValue == Carlos[i]) {

          supemail.rawValue

= "first.last@domain.com";

          found

= true;

     }

}

if

(found == false){

     for (var i=0; i<Corey.length;i++ ){

          if(email.rawValue == Corey[i]) {

               supemail.rawValue

= "last.first@domain.com";

               found

= true;

          }

     }

}

if

(found == false){

    for (var i=0; i<Ryan.length;i++ ){

          if(email.rawValue == Ryan[i]) {

               supemail.rawValue

= everyone@domain.com;

               found = true;

          }    

     }

}

Avatar

Level 3

OUTSTANDING! I had a little trouble with the var 'found' so I changed it slightly but the following code (almost completley based on your revisions) now works:


var Carlos = Array("first.last@domain.com","first.last@domain.com");
var Corey = Array("first.last@domain.com","first.last@domain.com");
var Ryan = Array("first.last@domain.com","first.last@domain.com");

var wasitfound = false;

for (var i=0; i<Carlos.length;i++ ){
    if(email.rawValue == Carlos[i]) {
        supemail.rawValue = "carlos@domain.com";
        wasitfound = "carlos@domain.com";
    }
}
    if (wasitfound == false){

        for (var i=0; i<Corey.length;i++ ){
            if(email.rawValue == Corey[i]) {
                supemail.rawValue = "corey@domain.com";
                wasitfound = "corey@domain.com";
            }
        }
            }

etc...