日期比较&从对象数组中过滤数据。

问题描述 投票:0回答:1

我有以下来自MongoDB的对象数组,TTL为29天(假设数组中包含大量的对象)

array1 = [
  {
   _id: "391A", // unique id 
   appData: { key1: "12345", key2: "abcd", key3: "dskjf" },
   createdOn: ISODate("2020-05-03T05:22:26.326Z"),
   customerData: { fName: "stack", lName: "overflow" }
  },
  {
    _id: "485B", // unique id 
    appData: { key1: "ewrf", key2: "hgmjn", key3: "rdffd" },
    createdOn: ISODate("2020-12-03T05:22:26.326Z"),
    customerData: { fName: "fd", lName: "xcv" }
  },
  {
     _id: "DFB5", // unique id 
     appData: { key1: "dfvf", key2: "hjhgg", key3: "sxxcxc" },
     createdOn: ISODate("2020-12-03T05:22:26.326Z"),
     customerData: { fName: "vihn", lName: "jkmv" }
  }
];

我有另一个大对象数组,我将其转换为Map。[ 假设startDate & endDate的差异是30天 ] 。

Map {
 "896J" => { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "961G" => { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "391A" => { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "BB86" => { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "NJ90" => { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "485B" => { startDate: "2020-02-14T05:22:26.326Z", endDate: "2020-03-15T05:22:26.326Z" },
 "KLP6" => { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
}

我需要检查array1中的_id与Map中的大数据是否匹配。如果_id和Map键匹配,那么在Map中检查currentDate是否小于或等于(endDate - 1)。只比较日期部分,日期中的时间不需要。如果检查匹配,则新建一个对象数组,输出如下。

finalArray = [
 { createdAt: "startDateValue", key1: "12345", fName: "overflow"},
 { createdAt: "startDateValue", key1: "ewrf", fName: "fd"},
];

我正处于学习阶段,所以非常感谢任何帮助。Moment或原生javascript日期都可以。

先谢谢你。

javascript node.js ecmascript-6 momentjs
1个回答
1
投票

简单的for循环。

ISODate=Date;

var array1 = [
  {
   _id: "391A", // unique id 
   appData: { key1: "12345", key2: "abcd", key3: "dskjf" },
   createdOn: ISODate("2020-05-03T05:22:26.326Z"),
   customerData: { fName: "stack", lName: "overflow" }
  },
  {
    _id: "485B", // unique id 
    appData: { key1: "ewrf", key2: "hgmjn", key3: "rdffd" },
    createdOn: ISODate("2020-12-03T05:22:26.326Z"),
    customerData: { fName: "fd", lName: "xcv" }
  },
  {
     _id: "DFB5", // unique id 
     appData: { key1: "dfvf", key2: "hjhgg", key3: "sxxcxc" },
     createdOn: ISODate("2020-12-03T05:22:26.326Z"),
     customerData: { fName: "vihn", lName: "jkmv" }
  }
];

var map = new Map(Object.entries({
 "896J" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "961G" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "391A" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "BB86" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "NJ90" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "485B" : { startDate: "2020-02-14T05:22:26.326Z", endDate: "2020-03-15T05:22:26.326Z" },
 "KLP6" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
}));


var now = new Date(new Date().getTime()+24*3600*1000).
            toISOString().slice(0,10);
var res = [];
for ( var i = 0 ; i < array1.length ; i++ ) {
  const x = array1[i];
  const y = map.get(x._id);
  if ( y && now < y.endDate )
    res.push({
     createdAt: y.startDate,
     key1: x.appData.key1, fName: x.customerData.fName
    });
}
console.log(res);

Reduce:

ISODate=Date;

var array1 = [
  {
   _id: "391A", // unique id 
   appData: { key1: "12345", key2: "abcd", key3: "dskjf" },
   createdOn: ISODate("2020-05-03T05:22:26.326Z"),
   customerData: { fName: "stack", lName: "overflow" }
  },
  {
    _id: "485B", // unique id 
    appData: { key1: "ewrf", key2: "hgmjn", key3: "rdffd" },
    createdOn: ISODate("2020-12-03T05:22:26.326Z"),
    customerData: { fName: "fd", lName: "xcv" }
  },
  {
     _id: "DFB5", // unique id 
     appData: { key1: "dfvf", key2: "hjhgg", key3: "sxxcxc" },
     createdOn: ISODate("2020-12-03T05:22:26.326Z"),
     customerData: { fName: "vihn", lName: "jkmv" }
  }
];

var map = new Map(Object.entries({
 "896J" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "961G" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "391A" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "BB86" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "NJ90" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "485B" : { startDate: "2020-02-14T05:22:26.326Z", endDate: "2020-03-15T05:22:26.326Z" },
 "KLP6" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
}));


var now = new Date(new Date().getTime()+24*3600*1000).
            toISOString().slice(0,10);

var res = array1.reduce( (res, x) => {
  const y = map.get(x._id);
  if ( y && now < y.endDate )
    res.push({
      createdAt: y.startDate,
      key1: x.appData.key1, fName: x.customerData.fName
    });
  return res;
},[]);

console.log(res);

FlatMap版本:

ISODate=Date;

var array1 = [
  {
   _id: "391A", // unique id 
   appData: { key1: "12345", key2: "abcd", key3: "dskjf" },
   createdOn: ISODate("2020-05-03T05:22:26.326Z"),
   customerData: { fName: "stack", lName: "overflow" }
  },
  {
    _id: "485B", // unique id 
    appData: { key1: "ewrf", key2: "hgmjn", key3: "rdffd" },
    createdOn: ISODate("2020-12-03T05:22:26.326Z"),
    customerData: { fName: "fd", lName: "xcv" }
  },
  {
     _id: "DFB5", // unique id 
     appData: { key1: "dfvf", key2: "hjhgg", key3: "sxxcxc" },
     createdOn: ISODate("2020-12-03T05:22:26.326Z"),
     customerData: { fName: "vihn", lName: "jkmv" }
  }
];

var map = new Map(Object.entries({
 "896J" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "961G" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "391A" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "BB86" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "NJ90" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "485B" : { startDate: "2020-02-14T05:22:26.326Z", endDate: "2020-03-15T05:22:26.326Z" },
 "KLP6" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
}));


var now = new Date(new Date().getTime()+24*3600*1000).
            toISOString().slice(0,10);
var res = array1.flatMap(x=> (y=map.get(x._id)) && now < y.endDate ? 
    {
     createdAt: y.startDate,
     key1: x.appData.key1, fName: x.customerData.lName
    } : []
); console.log(res);

转换为整数以便比较。

ISODate=Date;

var array1 = [
  {
   _id: "391A", // unique id 
   appData: { key1: "12345", key2: "abcd", key3: "dskjf" },
   createdOn: ISODate("2020-05-03T05:22:26.326Z"),
   customerData: { fName: "stack", lName: "overflow" }
  },
  {
    _id: "485B", // unique id 
    appData: { key1: "ewrf", key2: "hgmjn", key3: "rdffd" },
    createdOn: ISODate("2020-12-03T05:22:26.326Z"),
    customerData: { fName: "fd", lName: "xcv" }
  },
  {
     _id: "DFB5", // unique id 
     appData: { key1: "dfvf", key2: "hjhgg", key3: "sxxcxc" },
     createdOn: ISODate("2020-12-03T05:22:26.326Z"),
     customerData: { fName: "vihn", lName: "jkmv" }
  }
];

var map = new Map(Object.entries({
 "896J" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "961G" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "391A" : { startDate: "2020-04-30T05:22:26.326Z", endDate: "2020-05-30T05:22:26.326Z" },
 "BB86" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "NJ90" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
 "485B" : { startDate: "2020-02-14T05:22:26.326Z", endDate: "2020-03-15T05:22:26.326Z" },
 "KLP6" : { startDate: "2020-12-03T05:22:26.326Z", endDate: "2020-12-03T05:22:26.326Z" },
}));

const datetoint = d => (d.slice(0,4)+d.slice(5,7)+d.slice(8,10))|0;

var now = datetoint(new Date(new Date().getTime()+24*3600*1000).
            toISOString().slice(0,10));
var res = [];
for ( var i = 0 ; i < array1.length ; i++ ) {
  const x = array1[i];
  const y = map.get(x._id);
  if ( y && now <= datetoint(y.endDate) )
    res.push({
     createdAt: y.startDate,
     key1: x.appData.key1, fName: x.customerData.fName
    });
}
console.log(res);
© www.soinside.com 2019 - 2024. All rights reserved.