Сравнение дат и фильтрация данных из массива объектов - PullRequest
0 голосов
/ 03 мая 2020

У меня есть следующий массив объектов, поступающих из 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" },
}

Мне нужно проверить, совпадает ли _id в массиве1 с большими данными в Map. Если _id и ключ Map совпадают, то проверьте в Map, меньше или равно currentDate (endDate - 1). Сравните только часть даты, время в дате не требуется. Если проверка совпадает, создайте новый массив объектов со следующим выводом.

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

Я нахожусь в фазе обучения, поэтому любая помощь будет очень признательна. Момент или родной javascript дата в порядке.

Заранее спасибо.

1 Ответ

1 голос
/ 03 мая 2020

Простой для л oop:

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);

Уменьшение:

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);

версия плоской карты:

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);
...