3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個贊
您可以對數(shù)組應(yīng)用二進(jìn)制搜索。前提是您的數(shù)組已排序。->O(log(n))
[obj1,obj2,obj3....obj100]
測試中間的對象(obj50),然后決定是否必須在一半[obj1...obj49]或一半中進(jìn)行搜索,[obj51...obj100]
否則您可以將對象(事件)傳遞到其他數(shù)據(jù)結(jié)構(gòu)中,例如樹。->O(log(n)) 只是循環(huán)遍歷整個數(shù)組不會有效率,但如果你不重復(fù)它太多也可以。但是從一開始就對數(shù)組進(jìn)行排序?qū)⑹亲詈玫慕鉀Q方案。
編輯:以下代碼顯示了二進(jìn)制搜索實(shí)現(xiàn)的基本示例。
const events = [{
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"
},
{
eventTitle: "Event title 4",
eventId: "xyz4@google.com",
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: "xyz5@google.com",
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));

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個贊
您可以創(chuàng)建映射器實(shí)用程序。首先,它會循環(huán)一次,O(n). 以后所有的電話都會O(1)
我有工作代碼:
https://gist.github.com/deepakshrma/4b6a0a31b4582d6418ec4f76b7439781
function Mapper(array , key){
this.map = array.reduce(function(map, item){
var val = item[key];
if(!map[val]){
map[val] = [];
}
map[val].push(item);
return map;
},{});
}
Mapper.FIRST_INDEX = 0;
Mapper.prototype.find = function find(key){
return this.map[key] && this.map[key][Mapper.FIRST_INDEX]//return blank array
};
Mapper.prototype.findAll = function find(key, returnUndefined){
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"},
{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"}];
//How to use
var userMapper = new Mapper(users , 'eventTitle');
console.log(userMapper.find("Event title 2"));
var userToFind = ["Event title 3", "Event title 2"];
var reqUsers = userToFind.map(function(name){
return userMapper.find(name);
});
console.log(reqUsers);

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個贊
如果你只有數(shù)組,你別無選擇,只能搜索它。
但是,如果您將不得不多次搜索它,您可以通過它進(jìn)行一次遍歷以生成 Map,以便后續(xù)搜索是次線性的(比使用循環(huán)搜索數(shù)組更快)。你會這樣做:
const map = new Map(theArray.map(entry => [entry.eventTitle, entry.startDate]));
然后按標(biāo)題獲取是:
const startDate = map.get("some title");
現(xiàn)場示例:
(你可以用一個對象而不是一個 Map 來做同樣的事情(只要確保創(chuàng)建它,Object.create(null)
這樣它就沒有原型),但 Map 是專門為此設(shè)計的。)
請注意,此示例假定每個標(biāo)題只有一個事件。如果可能有多個,您需要以不同的方式構(gòu)建地圖,以便將您指向該標(biāo)題的唯一條目數(shù)組。
添加回答
舉報