Home > OS >  Why is the final array returning 7 rows of the last array compiled instead of the 7 unique array val
Why is the final array returning 7 rows of the last array compiled instead of the 7 unique array val

Time:10-17

Sorry, I'm pretty new to this but I do extras casting and need to get stats of who and what types I have working and would like build the template so I can reference it later to start counting and displaying the stats.

My code:

function myFunction ()  {
  var UNIQUE_ETHNIC = ["w","b","h","a","m","e","o"];
  var UNIQUE_GENDER = ["m","f"];
  var UNIQUE_AGE = ["20","30","40","50","60","70"];
  var egaArray =  calculateEga(UNIQUE_ETHNIC, UNIQUE_GENDER, UNIQUE_AGE); 
}

function calculateEga(ethnicUnique, genderUnique, ageUnique) {  
  var returnedArray = [[],[]];
  var tempArray = [];
  for (eU = 0; eU < ethnicUnique.length; eU  ) {
    var columnCounter = 0;
    for (gU = 0; gU < genderUnique.length; gU  ) {    
      for (aU = 0; aU < ageUnique.length; aU  ) {
        tempArray[columnCounter] = ethnicUnique[eU] genderUnique[gU] ageUnique[aU];
        columnCounter  ; //used so male & female on one line
        }
    }
    Logger.log(tempArray); //this gives the right array here each time
    returnedArray[eU] = tempArray; 
    //returnedArray[eU] = ['ega: ' tempArray];  //outputs what I kind of need

  }
  Logger.log(returnedArray); // 7 dupes of the last compiled array
  return returnedArray;
}

--- The 1st Logger.log spits out the correct tempArray values however once I stick them in the returnedArray it just gives me 7 dupes of the last compiled array thus the last Logger.log is giving this:

[[om20, om30, om40, om50, om60, om70, of20, of30, of40, of50, of60, of70],

[om20, om30, om40, om50, om60, om70, of20, of30, of40, of50, of60, of70],

[om20, om30, om40, om50, om60, om70, of20, of30, of40, of50, of60, of70],

[om20, om30, om40, om50, om60, om70, of20, of30, of40, of50, of60, of70],

[om20, om30, om40, om50, om60, om70, of20, of30, of40, of50, of60, of70],

[om20, om30, om40, om50, om60, om70, of20, of30, of40, of50, of60, of70],

[om20, om30, om40, om50, om60, om70, of20, of30, of40, of50, of60, of70]]

But if I use that commented out 'ega: ' call instead I get the much closer to what I'm aiming for version:

[ega: wm20,wm30,wm40,wm50,wm60,wm70,wf20,wf30,wf40,wf50,wf60,wf70],

[ega: bm20,bm30,bm40,bm50,bm60,bm70,bf20,bf30,bf40,bf50,bf60,bf70],

[ega: hm20,hm30,hm40,hm50,hm60,hm70,hf20,hf30,hf40,hf50,hf60,hf70],

[ega: am20,am30,am40,am50,am60,am70,af20,af30,af40,af50,af60,af70],

[ega: mm20,mm30,mm40,mm50,mm60,mm70,mf20,mf30,mf40,mf50,mf60,mf70],

[ega: em20,em30,em40,em50,em60,em70,ef20,ef30,ef40,ef50,ef60,ef70],

[ega: om20,om30,om40,om50,om60,om70,of20,of30,of40,of50,of60,of70]]

Would like to get the later version but w/out the 'ega: '

Thank you for any help here!

CodePudding user response:

I thought that in your situation, the value of tempArray might be put to the array of returnedArray as the pass by reference. From your sample values, it seems that all values are the last array of tempArray. By this, when 'ega: ' tempArray is used, the pass by value is run and the expected array is obtained. I thought that this might be the reason for your issue. In this case, how about the following modification?

From:

returnedArray[eU] = tempArray; 

To:

returnedArray[eU] = tempArray.slice();

or

returnedArray[eU] = tempArray.concat();

or

returnedArray[eU] = [...tempArray];
  • In this case, I thought that other methods are also existing.

Testing:

Show code snippet

function myFunction() {
  var UNIQUE_ETHNIC = ["w", "b", "h", "a", "m", "e", "o"];
  var UNIQUE_GENDER = ["m", "f"];
  var UNIQUE_AGE = ["20", "30", "40", "50", "60", "70"];
  var egaArray = calculateEga(UNIQUE_ETHNIC, UNIQUE_GENDER, UNIQUE_AGE);
  console.log(egaArray)
}

function calculateEga(ethnicUnique, genderUnique, ageUnique) {
  var returnedArray = [[], []];
  var tempArray = [];
  for (eU = 0; eU < ethnicUnique.length; eU  ) {
    var columnCounter = 0;
    for (gU = 0; gU < genderUnique.length; gU  ) {
      for (aU = 0; aU < ageUnique.length; aU  ) {
        tempArray[columnCounter] = ethnicUnique[eU]   genderUnique[gU]   ageUnique[aU];
        columnCounter  ;
      }
    }
    returnedArray[eU] = tempArray.slice();
  }
  return returnedArray;
}

myFunction();
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

References:

  • Related