Pimo
Pimo

Reputation: 141

Why array is getting bigger after loop?

I'm trying to change a color of background cells in google sheets so I wrote a code that I guess should do the job after putting into .setBackgrounds() (to mark duplicates)

But I have no idea why after looping calkiemNowa array is getting few times bigger then the original arrays.

Values length is around 4k and nowaArr has 200 elements, but the output calkiemNowa has something around 120k elements so I am getting error of too many values...

The goal seems to be simple: check if in the Values are the same elements as in nowaArr and mark them red.

Why calkiemNowa is getting that big? is it because of double loop?

    var nowaArr =[[1376064101], [1376064201], [1376613201], [1376613301], [1376613401], [1376613501], [1376613601], [1376613701], [1376613801], [1376613901], [1216987501], [1256524001], [1256639901], [1296132901], [1296133001], [1296133101], [1296133201]];
    var values = [[1326M76501], [1326M76301], [1326M76101], [1326M76901], [1326M76701], [1296133901], [1296133701], [1296133801], [1326H14201], [1216991801], [1236N12801], [1236F76701], [1326H16101], [1376616801], [1236F78101], [1236N13401], [1326G44401], [1236F79301], [1326848401], [1326N08801], [1326N08601], [1326N12601], [1326N09801], [1326N14701], [1326N15401], [1326N09601], [1326N09001], [1326N14001], [1326N11901],[1296133201]];

function checkArrays(){

var calkiemNowa = [];
for(var i = 0; i < values.length; i++){
  for(var j = 0; j < nowaArr.length; j++){
    if(values[i][0] === nowaArr[j][0]){
      calkiemNowa.push('red');
    }else{
      calkiemNowa.push('null');
    }
  }
}
return calkiemNowa;
}

Upvotes: 1

Views: 72

Answers (1)

lam
lam

Reputation: 565

I guess it works, you can have it a try

function checkArrays(){

var calkiemNowa = [];
loop1:
for(const value of values){
  loop2:
  for(const arr of nowaArr){
    if(value[0] === arr[0]){
      calkiemNowa.push('red');
      continue loop1;
    }
  }
  calkiemNowa.push(null);
}
return calkiemNowa;
}

Upvotes: 2

Related Questions