Home > Software design >  Find occurence by comparing 2 arrays
Find occurence by comparing 2 arrays

Time:03-31

function getResponseList () {
var listFormResponse = sheetNameFormResponse.getRange(2,columnOfDateTime(),sheetNameFormResponse.getLastRow()-1).getValues();
var lastRow = sheetName.getRange(2, 1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var listAvaApointment = sheetName.getRange(2,1,lastRow).getValues();
var count = listFormResponse.filter(function(listFormResponse) {
return listFormResponse == listAvaApointment[0][0];
});
Logger.log(listFormResponse);
Logger.log(listAvaApointment[0][0]);
Logger.log(count);
}

I am currently using array.filter().length to find ocurrence by comparing 2 arrays. However, the filter() function is not working properly and returns nothing by comparing 2 arrays.

執行記錄
上午11:09:13  通知  開始執行
上午11:09:14  資訊  [[Tue Mar 29 15:00:00 GMT 08:00 2022], 
[Wed Mar 30 14:30:00 GMT 08:00 2022], 
[Wed Mar 30 16:00:00 GMT 08:00 2022], 
[Wed Mar 30 15:30:00 GMT 08:00 2022], 
[Wed Mar 30 14:30:00 GMT 08:00 2022], 
[Wed Mar 30 10:00:00 GMT 08:00 2022], 
[Wed Mar 30 10:00:00 GMT 08:00 2022], 
[Wed Mar 30 10:00:00 GMT 08:00 2022]]
上午11:09:14  資訊  Wed Mar 30 10:00:00 GMT 08:00 2022
上午11:09:14  資訊  []
上午11:09:14  通知  執行完畢

CodePudding user response:

you are comparing [] to a date object. So it does not return a match. [ Wed Mar 30 2022 01:00:00 GMT-0400 (Eastern Daylight Time) ] Wed Mar 30 2022 01:00:00 GMT-0400 (Eastern Daylight Time) 'object' 'object'

function getResponseList () {
    var listFormResponse = sheetNameFormResponse.getRange(2,columnOfDateTime(),sheetNameFormResponse.getLastRow()-1).getValues();
    var lastRow = sheetName.getRange(2, 1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
    var listAvaApointment = sheetName.getRange(2,1,lastRow).getValues();
    var count = listFormResponse.filter(function(listFormResponse) {
    return listFormResponse[0] == listAvaApointment[0][0];
    });
    Logger.log(listFormResponse);
    Logger.log(listAvaApointment[0][0]);
    Logger.log(count);
}

CodePudding user response:

Description

There is no way for me to test this because I don't know what your data is but locigally it should work. And I don't know which element of listFormResponse you want to test against.

listFormResponse is a 2D array, so in your Array.filter callback function the parameter to the function is the row of the array, I call it formResponse. Then formResponse is compared to the listAvaApointment array creating another array I'll call foundResponse containing each of the rows that passed the test.

Script

function getResponseList () {
  var listFormResponse = sheetNameFormResponse.getRange(2,columnOfDateTime(),sheetNameFormResponse.getLastRow()-1).getValues();
  var lastRow = sheetName.getRange(2, 1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
  var listAvaApointment = sheetName.getRange(2,1,lastRow).getValues();
  var foundResponse = listFormResponse.filter(function(formResponse) {
    formResponse[0] == listAvaApointment[0][0];
  });
  Logger.log(listFormResponse);  // A 2D array
  Logger.log(listAvaApointment[0][0]);
  Logger.log(foundResponse.length); // A 2D array
}

Reference

  • Related