簡體   English   中英

比較兩個數組的相交值

[英]comparing two arrays for intersecting values

我正在嘗試使用JavaScript比較2個不同的數組並測試相交的值。

該陣列包含每周7天的可用性時間范圍。 在下方的availability中,每個鍵代表從星期日開始的一周中的一天。 因此,鍵0 =周日,鍵1 =星期一.....最大鍵數為6,即周六

var availability = [["8:30AM-12PM","2PM-6PM"],
["6:15AM-9:30AM","1PM-4PM","8PM-11:15PM"],[],["9AM-9PM"]];

下面的陣列need1包含一周中特定日期(從周日{key0}到周六{key6}的任何地方)的需求時間范圍。 它使用與上述availability陣列相同的密鑰格式。 need1陣列應在availability陣列中列出的時間和日期內匹配。 因此,在將need1availability進行比較need1 ,應該找到一個匹配項。

var need1 = [["9AM-11:30AM","3PM-5PM"],[],[],["2PM-6:30PM"]]; //matches

下面的陣列need2是不匹配的示例,因為需求時間范圍不在availability陣列中列出的時間和天數范圍內。 因此,在將need2availability進行比較時, need2匹配項。

var need2 = [["7:30AM-11:30AM"],[],[],["2PM-6:30PM", "8PM-10:30PM"]]; //does not match

我試圖弄清楚如何比較這兩個數組以使需求范圍與可用性范圍相匹配。 這意味着需求范圍必須完全適合每天的可用性范圍。 比較時,我要查找的只是一個簡單的true或false返回值。 true =匹配, false =不匹配。 但是,我不知道如何有效地做到這一點。 任何幫助,將不勝感激!

另外,如果需要的話,如果可以比較它們,我可以用其他格式布置數組值。 即使用日期值而不是字符串,或者使用24小時格式而不是12小時格式

我將在這里冒險,采取的方法與您開始時的方法略有不同,但這為您的基本問題提供了解決方案:可用時間與所需時間之間是否存在匹配?

在介紹代碼之前,這里有一些建議:
1.在頂層使用對象,而不是數組。 使用數組位置有意義是危險的。 而是利用JavaScript對象文字的功能。
2.分開開始和結束時間,不要將它們放在同一字符串中。
3.利用本機Array方法的功能。

我提出了這些建議,並提供了以下代碼段,因為您似乎在解決問題的方式上有一定的空間。

更新已更改每個請求的假設。 如果每個所需的開始/時隙在可用性對象中找到匹配的時隙,則僅返回給定日期的匹配。

如下所示的內容應該可以讓您到達想要去的地方。 這將返回匹配需求的對象。

 /* structure your schedule as objects with arrays of objects of start/stop times */ const availability1 = { sunday: [ { start: '08:30', stop: '12:00' }, { start: '14:00', stop: '18:00' } ], monday: [{ start: '06:25', stop: '11:45' }], wednesday: [] }; const need1 = { // will be matched, every needed time slot is within an availability slot // on the same day sunday: [ { start: '09:45', // has a match stop: '12:00' }, { start: '14:00', // has a match stop: '18:00' } ], monday: [ // will not be matched because... { start: '14:00', // has NO match stop: '16:00' }, { start: '07:00', // has a match stop: '10:00' } ], tuesday: [] }; const getMinutes = (timeString) => { const timeParts = timeString.split(':'); const hours = Number(timeParts[0]); const minutes = Number(timeParts[1]); const timeInMinutes = (hours * 60) + minutes; // console.log(`timeInMinutes: ${timeInMinutes}`); return timeInMinutes; } const isTimeMatch = (availabilityTime, needTime) => { const availStart = getMinutes(availabilityTime.start); const availStop = getMinutes(availabilityTime.stop); const needStart = getMinutes(needTime.start); const needStop = getMinutes(needTime.stop); console.log(`Availibility ${availabilityTime.start} (${availStart}) - ${availabilityTime.stop} (${availStop})`) console.log(`Need ${needTime.start} (${needStart}) - ${needTime.stop} (${needStop})`) const startTimeMatch = availStart <= needStart; const stopTimeMatch = availStop >= needStop; const isMatch = startTimeMatch && stopTimeMatch; console.log(`is match: ${isMatch}`); return isMatch; }; const compareDays = (availTimes, needTimes) => { return needTimes.map((needTime, i) => { const matches = availTimes.filter((availTime) => { return (isTimeMatch(availTime, needTime)); }); needTime.match = matches.length > 0; return needTime; }).filter((needTime, i, allNeedTimes) => { return (allNeedTimes.every((i) => i.match === true)); }); } function findMatches(availability, need) { const matches = Object.keys(availability).reduce((accumulator, day) => { if (availability[day] && need[day]) { console.log(`Possible matches found on ${day}`) const matches = compareDays(availability[day], need[day]); if (matches.length > 0) { accumulator[day] = matches; } } return accumulator; }, {}); return (Object.keys(matches).length === 0) ? false : matches; } const matchedTimes = findMatches(availability1, need1); if (matchedTimes) { console.log('The following needs match availability:'); console.log(matchedTimes); } else { console.log('No matches found'); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM