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

给定这个数组对象:


[{eventTitle=Event title 1, eventId=xyz1@google.com, 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=xyz2@google.com, 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=xyz3@google.com, startDate=Fri Mar 20 00:00:00 GMT+01:00 2020, endDate=Sat Mar 21 00:00:00 GMT+01:00 2020},

 .

 .

 .]

如何在不循环/搜索数组的情况下有效地检索与startDate某个特定的对应?eventTitle例如,我有Event title 2并且想要得到Tue Mar 19 00:00:00 GMT+01:00 2019.


编辑:


数组对象按 排序startDate。


偶然的你
浏览 144回答 3
3回答

明月笑刀无情

您可以对数组应用二进制搜索。前提是您的数组已排序。->O(log(n))[obj1,obj2,obj3....obj100]测试中间的对象(obj50),然后决定是否必须在一半[obj1...obj49]或一半中进行搜索,[obj51...obj100]否则您可以将对象(事件)传递到其他数据结构中,例如树。->O(log(n)) 只是循环遍历整个数组不会有效率,但如果你不重复它太多也可以。但是从一开始就对数组进行排序将是最好的解决方案。编辑:以下代码显示了二进制搜索实现的基本示例。const events = [{&nbsp; &nbsp; eventTitle: "Event title 1",&nbsp; &nbsp; eventId: "xyz1@google.com",&nbsp; &nbsp; startDate: "Sun Mar 18 00:00:00 GMT+01:00 2018",&nbsp; &nbsp; endDate: "Mon Mar 19 00:00:00 GMT+01:00 2018"&nbsp; },&nbsp; {&nbsp; &nbsp; eventTitle: "Event title 2",&nbsp; &nbsp; eventId: "xyz2@google.com",&nbsp; &nbsp; startDate: "Tue Mar 19 00:00:00 GMT+01:00 2019",&nbsp; &nbsp; endDate: "Wed Mar 20 00:00:00 GMT+01:00 2019"&nbsp; },&nbsp; {&nbsp; &nbsp; eventTitle: "Event title 3",&nbsp; &nbsp; eventId: "xyz3@google.com",&nbsp; &nbsp; startDate: "Fri Mar 20 00:00:00 GMT+01:00 2020",&nbsp; &nbsp; endDate: "Sat Mar 21 00:00:00 GMT+01:00 2020"&nbsp; },&nbsp; {&nbsp; &nbsp; eventTitle: "Event title 4",&nbsp; &nbsp; eventId: "xyz4@google.com",&nbsp; &nbsp; startDate: "Fri Mar 21 00:00:00 GMT+01:00 2021",&nbsp; &nbsp; endDate: "Sat Mar 22 00:00:00 GMT+01:00 2021"&nbsp; },&nbsp; {&nbsp; &nbsp; eventTitle: "Event title 5",&nbsp; &nbsp; eventId: "xyz5@google.com",&nbsp; &nbsp; startDate: "Fri Mar 22 00:00:00 GMT+01:00 2022",&nbsp; &nbsp; endDate: "Sat Mar 23 00:00:00 GMT+01:00 2022"&nbsp; }];function binarySearch(array, value, borderLeft, borderRight) {&nbsp; if (borderLeft <= borderRight) {&nbsp; &nbsp; var index = Math.floor((borderLeft + borderRight) / 2);&nbsp; &nbsp; var number = getNumberFromTitle(array[index].eventTitle);&nbsp; &nbsp; if (number == value) {&nbsp; &nbsp; &nbsp; return array[index].startDate;&nbsp; &nbsp; } else if (number > value) {&nbsp; &nbsp; &nbsp; return binarySearch(array, value, borderLeft, index - 1);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; return binarySearch(array, value, index + 1, borderRight);&nbsp; &nbsp; }&nbsp; } else {&nbsp; &nbsp; return null;&nbsp; }}function getNumberFromTitle(title) {&nbsp; var tmp = title.split(" ");&nbsp; return tmp[tmp.length - 1];}console.log(binarySearch(events, 4, 0, events.length - 1));

哆啦的时光机

您可以创建映射器实用程序。首先,它会循环一次,O(n). 以后所有的电话都会O(1)我有工作代码:https://gist.github.com/deepakshrma/4b6a0a31b4582d6418ec4f76b7439781function Mapper(array , key){&nbsp; &nbsp; this.map = array.reduce(function(map, item){&nbsp; &nbsp; &nbsp; &nbsp; var val = item[key];&nbsp; &nbsp; &nbsp; &nbsp; if(!map[val]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map[val] = [];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; map[val].push(item);&nbsp; &nbsp; &nbsp; &nbsp; return map;&nbsp; &nbsp; },{});}Mapper.FIRST_INDEX = 0;Mapper.prototype.find = function find(key){&nbsp; &nbsp; return this.map[key] && this.map[key][Mapper.FIRST_INDEX]//return blank array};Mapper.prototype.findAll = function find(key, returnUndefined){&nbsp; &nbsp; return this.map[key] && this.map[key] || (returnUndefined? undefined: []);//return blank array};var users = [{eventTitle:"Event title 1", eventId:"xyz1@google.com", startDate:"Sun Mar 18 00:00:00 GMT+01:00 2018", endDate:"Mon Mar 19 00:00:00 GMT+01:00 2018"},&nbsp;{eventTitle:"Event title 2", eventId:"xyz2@google.com", startDate:"Tue Mar 19 00:00:00 GMT+01:00 2019", endDate:"Wed Mar 20 00:00:00 GMT+01:00 2019"},&nbsp;{eventTitle:"Event title 3", eventId:"xyz3@google.com", startDate:"Fri Mar 20 00:00:00 GMT+01:00 2020", endDate:"Sat Mar 21 00:00:00 GMT+01:00 2020"}];//How to usevar userMapper = new Mapper(users , 'eventTitle');console.log(userMapper.find("Event title 2"));&nbsp;var userToFind = ["Event title 3", "Event title 2"];var reqUsers =&nbsp; userToFind.map(function(name){&nbsp; &nbsp; return userMapper.find(name);});console.log(reqUsers);

慕容708150

如果你只有数组,你别无选择,只能搜索它。但是,如果您将不得不多次搜索它,您可以通过它进行一次遍历以生成 Map,以便后续搜索是次线性的(比使用循环搜索数组更快)。你会这样做:const&nbsp;map&nbsp;=&nbsp;new&nbsp;Map(theArray.map(entry&nbsp;=>&nbsp;[entry.eventTitle,&nbsp;entry.startDate]));然后按标题获取是:const&nbsp;startDate&nbsp;=&nbsp;map.get("some&nbsp;title");现场示例:显示代码片段(你可以用一个对象而不是一个 Map 来做同样的事情(只要确保创建它,Object.create(null)这样它就没有原型),但 Map 是专门为此设计的。)请注意,此示例假定每个标题只有一个事件。如果可能有多个,您需要以不同的方式构建地图,以便将您指向该标题的唯一条目数组。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript