Ajmul
Ajmul

Reputation: 37

Apps Script For Loop stuck at 0 in iteration although it is running

I have an array 'vnData' containing 5, 6 rows from where I am trying to extract 3rd column values (based on a criteria) and insert to a new array. Here is my code

for (odr = 0; odr < vnData.length; odr++){
    Logger.log(vnData);
    tempOdr = vnData[odr][3];
    Logger.log(odr);
    Logger.log(tempOdr);
    Logger.log(vnData[odr][3]);
    for(k = 0; k < vnData.length; k++){
      if(vnData[k][3] = tempOdr){
        odrVal = odrVal + vnData[k][11];
      }
    }
    if(odrVal > 0){
      affOdrSet.push(tempOdr);
    }
  Logger.log(affOdrSet);
  }

Logger gives right value of odr in Logger.log(odr); but in Logger.log(vnData[odr][3]); I am always getting a result where value of odr is 0.

So for each iteration I get value from first row. Please help what is wrong in it.

One more thing, if I log Logger.log(vnData[3][3]) in place of Logger.log(vnData[odr][3]) then for first iteration it gives me right value from row 4 but for all subsequent iterations even Logger.log(vnData[3][3]) gives value from first row which is really weird.

Upvotes: 0

Views: 86

Answers (1)

Wicket
Wicket

Reputation: 38434

The problem is the expression of the first if statement. Instead of

vnData[k][3] = tempOdr

use

vnData[k][3] === tempOdr

The above because = is the assign operator but it's very likely that instead of assigning tempOdr to vnData[k][3] what you want to compare them.

Upvotes: 1

Related Questions