gpt4 book ai didi

JavaScript:如何有效地将数组对象项检索到某个属性?

转载 作者:行者123 更新时间:2023-12-01 15:59:39 26 4
gpt4 key购买 nike

给定这个数组对象:

[{eventTitle=Event title 1, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4b1a2b1baa09db0e9acadaee594b3bbbbb3b8b1fab7bbb9" rel="noreferrer noopener nofollow">[email protected]</a>, startDate=Sun Mar 18 00:00:00 GMT+01:00 2018, endDate=Mon Mar 19 00:00:00 GMT+01:00 2018},
{eventTitle=Event title 2, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e78291828993ae83da9f9e9dd5a7808888808b82c984888a" rel="noreferrer noopener nofollow">[email protected]</a>, startDate=Tue Mar 19 00:00:00 GMT+01:00 2019, endDate=Wed Mar 20 00:00:00 GMT+01:00 2019},
{eventTitle=Event title 3, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d5c6d5dec4f9d48dc8c9ca83f0d7dfdfd7dcd59ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>, startDate=Fri Mar 20 00:00:00 GMT+01:00 2020, endDate=Sat Mar 21 00:00:00 GMT+01:00 2020},
.
.
.]

如何在不循环/搜索数组的情况下高效检索某个 eventTitle 对应的 startDate?例如,我有事件标题 2,并希望获取Tue Mar 19 00:00:00 GMT+01:00 2019

编辑:

数组对象按startDate排序。

最佳答案

您可以对数组应用二分搜索。前提是你的数组已排序。 -> O(log(n))

[obj1,obj2,obj3....obj100]

测试中间的对象(obj50),然后决定是否必须在半部 [obj1...obj49] 或半部 [obj51...obj100] 中搜索


否则,您可以将对象(事件)传递到其他数据结构(如树)中。 -> O(log(n))仅循环遍历整个数组效率不高,但如果不重复太多,也可以。但从头开始对数组进行排序将是最好的解决方案。

编辑:以下代码显示了二分搜索实现的基本示例。

const events = [{
eventTitle: "Event title 1",
eventId: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acd4d5d69deccbc3c3cbc0c982cfc3c1" rel="noreferrer noopener nofollow">[email protected]</a>",
startDate: "Sun Mar 18 00:00:00 GMT+01:00 2018",
endDate: "Mon Mar 19 00:00:00 GMT+01:00 2018"
},
{
eventTitle: "Event title 2",
eventId: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e2627246c1e39313139323b703d3133" rel="noreferrer noopener nofollow">[email protected]</a>",
startDate: "Tue Mar 19 00:00:00 GMT+01:00 2019",
endDate: "Wed Mar 20 00:00:00 GMT+01:00 2019"
},
{
eventTitle: "Event title 3",
eventId: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="463e3f3c7506212929212a236825292b" rel="noreferrer noopener nofollow">[email protected]</a>",
startDate: "Fri Mar 20 00:00:00 GMT+01:00 2020",
endDate: "Sat Mar 21 00:00:00 GMT+01:00 2020"
},
{
eventTitle: "Event title 4",
eventId: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aad2d3d09eeacdc5c5cdc6cf84c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>",
startDate: "Fri Mar 21 00:00:00 GMT+01:00 2021",
endDate: "Sat Mar 22 00:00:00 GMT+01:00 2021"
},
{
eventTitle: "Event title 5",
eventId: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a2223206f1a3d35353d363f74393537" rel="noreferrer noopener nofollow">[email protected]</a>",
startDate: "Fri Mar 22 00:00:00 GMT+01:00 2022",
endDate: "Sat Mar 23 00:00:00 GMT+01:00 2022"
}
];

function binarySearch(array, value, borderLeft, borderRight) {
if (borderLeft <= borderRight) {
var index = Math.floor((borderLeft + borderRight) / 2);
var number = getNumberFromTitle(array[index].eventTitle);
if (number == value) {
return array[index].startDate;
} else if (number > value) {
return binarySearch(array, value, borderLeft, index - 1);
} else {
return binarySearch(array, value, index + 1, borderRight);
}
} else {
return null;
}
}

function getNumberFromTitle(title) {
var tmp = title.split(" ");
return tmp[tmp.length - 1];
}

console.log(binarySearch(events, 4, 0, events.length - 1));

关于JavaScript:如何有效地将数组对象项检索到某个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60790688/

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