gpt4 book ai didi

javascript - 比较两个数组的相交值

转载 作者:行者123 更新时间:2023-12-03 01:24:19 25 4
gpt4 key购买 nike

我正在尝试使用 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 数组相同的 key 格式。 need1 数组应在 availability 数组中列出的时间和日期内匹配。因此,在将need1availability 进行比较时,应该会找到匹配项。

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

下面的数组 need2 是一个不匹配的示例,因为需求时间范围超出了 availability 数组中列出的时间和日期范围。因此,当将 need2availability 进行比较时,应该找不到匹配项。

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. 利用原生数组方法的强大功能。

我提出这些建议并提供以下代码片段,因为您在解决问题的方式上似乎有一些回旋余地。

已更新根据请求更改了假设。如果每个所需的开始/时间段在可用性对象中找到匹配的时间段,则仅返回给定日期的匹配项。

像下面这样的东西应该可以让你到达你想去的地方。这将返回匹配需求的对象。

/* 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');
}

关于javascript - 比较两个数组的相交值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51617187/

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