Home > Software engineering >  How to concatenate 2 multidimensional arrays matching criteria in JS?
How to concatenate 2 multidimensional arrays matching criteria in JS?

Time:03-19

How can I add 02 elements/columns to array1, if a couple of criteria are met in the following scenario?

noPedido = 1000
let ordersData = sheet1.getRange(2, 1, sheet1.getLastRow(), sheet1.getLastColumn()).getValues();
let deliveryData = sheet2.getRange(2, 1, sheet2.getLastRow(), 13).getValues();
for (let a = deliveryData .length - 1; a >= 0; a--) {
      for (let n = ordersData .length - 1; n >= 0; n--) {
        if (deliveryData[a][12] == noPedido && ordersData[n][11] == noPedido
          && deliveryData[a][1] == ordersData[n][1]) {
          let row = n   2;
          let qtds = [deliveryData [a][4], deliveryData [a][5]];
          sheet1.getRange(row, 24, 1, 2).setValues([qtds]);//Adds found data to adjacent columns
        }
      }
    }

If I had a 2D array ordersData, which I do, I wouldn't have to get the values so I could get its rows in sheet1, where I'm setting the values.

Thanks.

CodePudding user response:

From sheet1.getRange(row, 24, 1, 2).setValues([qtds]);//Adds found data to adjacent columns in your script, if you want to add qtds to the same row of ordersData[n], how about the following modification?

From:

sheet1.getRange(row, 24, 1, 2).setValues([qtds]);

To:

ordersData[n] = [...ordersData[n], ...qtds];
  • In this modification, the values of qtds is added to the same row of ordersData[n].
  • Related