gpt4 book ai didi

javascript - 在矩数组中查找 moment.js 对象?

转载 作者:行者123 更新时间:2023-12-02 21:51:10 25 4
gpt4 key购买 nike

我有一个 moment() moment.js 对象数组。这些对象包含日期和开始时间,任何给定日期可以有 N 个对象。

我无法弄清楚如何搜索数组并返回给定日期内的所有时刻。是否可以在不迭代整个数组的情况下做到这一点? (约500个对象)

var dateTimeList = [];
// Grabs a bunch of date\times converts them to moments, adds to array
dateTimeList.push(moment(data[i]["start_date"] + data[i]["begintime"], "YYYYMMDDHHmm"));

function dateSelected(someSpecificDay, dateTimeList){
var startDate = moment(someSpecificDay);
var endDate = startDate.endOf('day');
var matchingMoments = [];
// Stuck here, How can i use inArray(){} to add the moment if it falls within that particular day?
}

示例输入(为了清晰起见,更改了格式):

 12/31/2019 09:00
1/1/2020 13:00
1/1/2020 14:00
1/1/2020 08:00
1/3/2020 09:00

选择日期(选择日期 1/1)返回:

 1/1/2020 13:00
1/1/2020 14:00
1/1/2020 08:00

选择日期(选择日期 1/5)返回:

Nothing

最佳答案

Is it possible to do this without iterating through the entire array?

不,您必须迭代整个数组。无论您使用什么方法,它都会在后台迭代整个数组。

如果您想在不迭代的情况下执行此操作,您应该使用不同的数据结构,例如以天为键的对象。这样您就可以通过 dateTimeList["1/1"] 获取相应的对象。

关于javascript - 在矩数组中查找 moment.js 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60129160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com