简体   繁体   English

array.filter不返回数组

[英]array.filter not returning array

I am trying to search through an large array of data using a date column to return the array of the accounts that have the date I need. 我正在尝试使用日期列搜索大量数据,以返回具有我需要的日期的科目数组。 I can't get the array to populate with any data. 我无法使数组填充任何数据。 What am I missing? 我想念什么?

var accounts = accountInfo.getRange('A2:AJ').getValues();
var maxRow = accountInfo.getMaxRows();
var today = new Date();
today.setHours(0,0,0,0);
var todayPlus5 = addDays(today, 5);

function reviewPriorityFilter(){
  var filtered = accounts.filter(rowNumber2);
  accounts = filtered;
  Logger.log(filtered);
}

function rowNumber2(value){
  var fix = new Date(value[15]);
  fix.setHours(0,0,0,0);
  return fix === todayPlus5;
}

function addDays(date, days){
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

You can't use === to compare new object instances. 您不能使用===比较新的对象实例。 Two objects created with new will never === each other, even if they have the same "values", like the same internal date. new创建的两个对象永远不会彼此=== ,即使它们具有相同的“值”,例如相同的内部日期。 This question shows the proper way to check for exact date matching: 这个问题显示了检查确切日期匹配的正确方法:

date1.getTime() === date2.getTime()

So your rowNumber2 filter should return something more like: 因此,您的rowNumber2过滤器应返回类似以下内容的内容:

return fix.getTime() === todayPlus5.getTime();

Keep in mind this will only work if the times are exactly matching. 请记住,这仅在时间完全匹配时才有效。 If the filter is still showing an empty value, log both dates to see if they truly hold the exact same time. 如果过滤器仍然显示一个空值,请记录两个日期以查看它们是否确实具有完全相同的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM