0

I'm trying to filter in an array by data in another array using concat in a for loop. The elements of the following code are logging correctly, but the final array is logging an empty array.

function Shipments (){
  var app = SpreadsheetApp;
  var movementSS = app.getActiveSpreadsheet();
  var infoSheet = movementSS.getSheetByName("Update Info");
  var orderInfoSheet = movementSS.getSheetByName("Order Info");

  var targetSheet = movementSS.getSheetByName("Shipments");
  var ShipLogSS = app.openByUrl(URL).getSheetByName("Shipping Details");


  var ShipArr = ShipLogSS.getRange(3,1,ShipLogSS.getLastRow(),ShipLogSS.getLastColumn()).getValues().
    filter(function(item){if(item[1]!=""){return true}}).
    map(function(r){return [r[0],r[1],r[2],r[4],r[10],r[11],r[16],r[18],r[23]]});

  var supplierData = orderInfoSheet.getRange(3,6,orderInfoSheet.getLastRow(),1).getValues().
    filter(function(item){if(item[0]!=""){return true}});


  var supplierList = [];
  for (var i in supplierData) {
    var row = supplierData[i];
    var duplicate = false;
    for (var j in supplierList) {
      if (row.join() == supplierList[j].join()) {
        duplicate = true;
      }
    }
    if (!duplicate) {
      supplierList.push(row);
    }
  }

  var supplierFilter = [];
  for(var i = 0; i < supplierList.length; i++){
    var shipments = ShipArr.filter(function(item){if(item[4]===supplierList[i][0]){return true}});
    supplierFilter.concat(shipments);
  }

  Logger.log(supplierFilter);

}

Any help would be greatly appreciated!

1 Answer 1

2

You need to assign the result of concat onto the supplierFilter in order to see the changes in later iterations and in the outer scope.

You can also return the comparison done inside the .filter callback immediately, instead of an if statement - it looks a bit cleaner.

var supplierFilter = [];
for (var i = 0; i < supplierList.length; i++) {
    var shipments = ShipArr.filter(function (item) { return item[4] === supplierList[i][0]; });
    supplierFilter = supplierFilter.concat(shipments);
}
Logger.log(supplierFilter);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.