我需要过滤一个数组以获取今天或明天两个字段中至少一个落入的项。
在这种情况下,有两个列表:
数据库信息包含时间,但是时间与过滤器无关,仅与日期有关。
数据库字段如下:
我已经使它在以下不良代码和大量“禁止混合运算符”的警告下起作用。结果是正确的。
dir("${workspace}/transactions-test")
{
sh "${workspace}/transactions-test/run.sh"
}
如果有人有一种更清洁,更合规的方式,我将不胜感激。
答案 0 :(得分:0)
在不了解您开始使用的对象的格式和内容的情况下,或者在排序的另一端了解所需的格式和活动的情况下……
这是一种可以考虑的方法:
// we'll use this object as an example for what we might expect to receive
// coming back from the DB containing a table that has an id for each flight
// with a corresponding departure and arrival time in a Unix Millisecond
// Timestamp format as you've indicated:
let db_results = [
{ id: 1, depart: 1563991649216, arrive: 1564042049217 },
{ id: 2, depart: 1563995249217, arrive: 1564045649217 },
{ id: 3, depart: 1563998849217, arrive: 1564049249217 },
{ id: 4, depart: 1564002449217, arrive: 1564052849217 },
{ id: 5, depart: 1564006049217, arrive: 1564056449217 },
{ id: 6, depart: 1564009649217, arrive: 1564060049217 },
{ id: 7, depart: 1564013249217, arrive: 1564063649217 },
{ id: 8, depart: 1564016849217, arrive: 1564067249217 },
{ id: 9, depart: 1564020449217, arrive: 1564070849217 },
{ id: 10, depart: 1564024049217, arrive: 1564074449217 },
{ id: 11, depart: 1564027649217, arrive: 1564078049217 },
{ id: 12, depart: 1564031249217, arrive: 1564081649217 },
{ id: 13, depart: 1564034849217, arrive: 1564085249217 },
{ id: 14, depart: 1564038449217, arrive: 1564088849217 },
{ id: 15, depart: 1564078094050, arrive: 1564132094050 },
{ id: 16, depart: 1564081694050, arrive: 1564135694050 },
{ id: 17, depart: 1564085294050, arrive: 1564139294050 },
{ id: 18, depart: 1564088894050, arrive: 1564142894050 },
{ id: 19, depart: 1564092494050, arrive: 1564146494050 },
{ id: 20, depart: 1564096094050, arrive: 1564150094051 },
{ id: 21, depart: 1564099694050, arrive: 1564153694051 },
{ id: 22, depart: 1564103294050, arrive: 1564157294051 },
{ id: 23, depart: 1564106894050, arrive: 1564160894051 },
{ id: 24, depart: 1564110494050, arrive: 1564164494051 },
{ id: 25, depart: 1564114094050, arrive: 1564168094051 },
{ id: 26, depart: 1564117694050, arrive: 1564171694051 },
{ id: 27, depart: 1564121294050, arrive: 1564175294051 },
{ id: 28, depart: 1564124894050, arrive: 1564178894051 },
{ id: 29, depart: 1564128494050, arrive: 1564182494051 }
]
const td_start = moment().startOf('day')
const td_end = moment().endOf('day')
const tm_start = moment().add(1, 'days').startOf('day')
const tm_end = moment().add(1, 'days').endOf('day')
// set up some arrays to hold our ids that qualify for each bucket
let flight_today = []
let flight_tomorrow = []
// determine what group(s) our flights fall into (there will be some overlap since you've indicated depart OR arrive as a qualifier)
for (var i = db_results.length - 1; i >= 0; i--) {
if (moment(db_results[i].depart, 'x').isBetween(td_start, td_end) || moment(db_results[i].arrive, 'x').isBetween(td_start, td_end)) {
flight_today.push(db_results[i].id)
}
if (moment(db_results[i].depart, 'x').isBetween(tm_start, tm_end) || moment(db_results[i].arrive, 'x').isBetween(tm_start, tm_end)) {
flight_tomorrow.push(db_results[i].id)
}
}
console.log(flight_today)
// expected output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
console.log(flight_tomorrow)
// expected output: [24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
如果您能够共享有关输入数据格式和所需最终数据格式的更多详细信息,我很乐意进行一些更新。
答案 1 :(得分:0)
您可以简单地使用isSame
传递'day'
作为第二个参数。
检查一个时刻是否与另一个时刻相同。第一个参数将被瞬间解析(如果尚未解析的话)。
包括第二个参数时,它将匹配所有等于或更大的单位。传递
month
将检查month
和year
。传递day
将检查day
,month
和year
。像
moment#isAfter
和moment#isBefore
一样,moment#startOf
也支持moment#isSame
支持的任何时间单位。
这里有一个现场样本:
const _flights = [
{departure_date: 1564011360000, arrival_date: 1564026420000},
{departure_date: 1563963930000, arrival_date: 1564011360000},
{departure_date: 1563877530000, arrival_date: 1563963930000}
// etc
];
const today = moment();
const tomorrow = moment().add(1, 'day');
const flightsToday =
_flights &&
_flights.filter(
_flights =>
today.isSame(_flights.arrival_date, 'day') || today.isSame(_flights.departure_date, 'day')
);
const flightsTomorrow =
_flights &&
_flights.filter(
_flights =>
tomorrow.isSame(_flights.arrival_date, 'day') || tomorrow.isSame(_flights.departure_date, 'day')
);
console.log(flightsToday);
console.log(flightsTomorrow);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>