Trying to find a method that works in Google Apps Scripts, to compare two arrays and find the values missing in the second array.
I've tried several approaches but can't find one that works in GAS. Currently attempting with a for() loop and indexOf():
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.setActiveSheet(ss.getSheetByName('test'));
function TEST(){
var lastRow = sheet.getLastRow();
var orders = sheet.getRange(2,1,lastRow,1).getValues(); //[a,b,c,d]
var products = sheet.getRange(2, 2,lastRow,1).getValues(); //[a, b]
var missing = [];
for ( var i = 0 ; i < Object.keys(orders).length; i++){
if(products.indexOf(orders[i])<0){
missing.push(orders[i]);};
};
Logger.log(missing); //expect [c, d]
}
The source table has two columns to compare, and a 3rd column where the new 'missing' array should be stored.
orders products missing
a a c
b b d
c
d
I tried methods from several other posts but everything is using functions that aren't available in Google Apps Scripts.