Простой для л 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);