英文:
Grouping Array by another array 'createdAt' values
问题
我有来自我的 API 的两个数组。
我想要按 'createdAt' 分组 passArray,筛选条件为 Array2 的 'createdAt' 范围为 'opened and closed'。
我不知道是否应该使用 reduce 还是 filter?
我希望最终结果类似于以下内容:
2023-08-03T12:15:46.000Z:
{pass_id: 645, heure_debut: '13:48:00', heure_fin: '14:48:00', createdAt:"2023-08-03T13:48:00.000Z"}
{pass_id: 646, heure_debut: '14:40:00', heure_fin: '15:40:00', createdAt:"2023-08-03T14:40:00.000Z"},
2023-08-04T12:29:04.000Z:
{pass_id: 647, heure_debut: '20:30:00', heure_fin: '21:30:00', createdAt:"2023-08-04T20:30:00.000Z"}
{pass_id: 648, heure_debut: '13:00:00', heure_fin: '14:00:00', createdAt:"2023-08-04T13:00:00.000Z"}
2023-08-05T07:57:16.000Z:
{pass_id: 649, heure_debut: '16:25:00', heure_fin: '17:25:00', createdAt:"2023-08-05T16:25:00.000Z"}
{pass_id: 650, heure_debut: '18:30:00', heure_fin: '19:30:00', createdAt:"2023-08-05T18:30:00.000Z"}
最终目标是在 Array2 的 'opened and closed' 范围内获取 passArray 中的通行证数量。
使用 reduce?还是 filter?还是 map?我不知道。
英文:
i have 2 arrays from my Api
this
passArray:[
{pass_id: 645, heure_debut: '13:48:00', heure_fin: '14:48:00', createdAt:"2023-08-03T13:48:00.000Z"}
{pass_id: 646, heure_debut: '14:40:00', heure_fin: '15:40:00', createdAt:"2023-08-03T14:40:00.000Z"}
{pass_id: 647, heure_debut: '20:30:00', heure_fin: '21:30:00', createdAt:"2023-08-04T20:30:00.000Z"}
{pass_id: 648, heure_debut: '13:00:00', heure_fin: '14:00:00', createdAt:"2023-08-04T13:00:00.000Z"}
{pass_id: 649, heure_debut: '16:25:00', heure_fin: '17:25:00', createdAt:"2023-08-05T16:25:00.000Z"}
{pass_id: 650, heure_debut: '18:30:00', heure_fin: '19:30:00', createdAt:"2023-08-05T18:30:00.000Z"}
]
array2:[
{horaire_id: 5, operation: 'opened', user_id: 1, createdAt: '2023-08-03T12:15:46.000Z'}
{horaire_id: 7, operation: 'closed', user_id: 1, createdAt: '2023-08-04T12:21:29.000Z'}
{horaire_id: 8, operation: 'opened', user_id: 1, createdAt: '2023-08-04T12:29:04.000Z'}
{horaire_id: 9, operation: 'closed', user_id: 1, createdAt: '2023-08-05T07:57:16.000Z'}
{horaire_id: 10, operation: 'opened',user_id: 1, createdAt: '2023-08-05T08:04:48.000Z'}
{horaire_id: 11, operation: 'closed',user_id: 1, createdAt: '2023-08-06T03:06:56.000Z'}
]
i want to groupe passArray by 'createdAt' filtered by Array2 'createdAt' interval 'opened and closed'
i dont know if i must to use reduce or filter ?
I wish to have as a result something like that
2023-08-03T12:15:46.000Z :
{pass_id: 645, heure_debut: '13:48:00', heure_fin: '14:48:00', createdAt:"2023-08-03T13:48:00.000Z"}
{pass_id: 646, heure_debut: '14:40:00', heure_fin: '15:40:00', createdAt:"2023-08-03T14:40:00.000Z"},
2023-08-04T12:29:04.000Z:
{pass_id: 647, heure_debut: '20:30:00', heure_fin: '21:30:00', createdAt:"2023-08-04T20:30:00.000Z"}
{pass_id: 648, heure_debut: '13:00:00', heure_fin: '14:00:00', createdAt:"2023-08-04T13:00:00.000Z"}
2023-08-05T07:57:16.000Z:
{pass_id: 649, heure_debut: '16:25:00', heure_fin: '17:25:00', createdAt:"2023-08-05T16:25:00.000Z"}
{pass_id: 650, heure_debut: '18:30:00', heure_fin: '19:30:00', createdAt:"2023-08-05T18:30:00.000Z"}
the final goal is to obtain at the end the number of pass in passArray in an interval of opened and closed of array2.
thinks
reduce ? or filter ? or map ? i dont know
答案1
得分: 0
理想情况下,我们能够看到你目前的进展,这样我们可以提供建议或潜在解决方案。鉴于这一点不存在,我将为你提供我处理此问题的想法。
此函数首先从 `array2` 创建了一个区间数组。然后它使用 reduce 遍历 `passArray`。对于 `passArray` 中的每个元素,它使用 `filter` 找到 `array2` 中与元素的 `createdAt` 日期相符的区间。然后将元素添加到正在创建的对象中的相应数组中。
请记住,在使用此函数之前,请确保 `array2` 按 `createdAt` 属性以升序排序。此函数假定 `array2` 中的每个 'opened' 操作后面都会有一个 'closed' 操作。
此函数创建的最终对象将具有作为 `array2` 区间起始日期的键,其值是 `passArray` 中在这些区间内的元素数组。你可以通过获取这些数组的长度来得到每个区间内的通行证数量。
英文:
Ideally, we'd be able to see what you have so far so we can offer advice or a potential solution from that. Since that is not present, I will give you an idea of what I would do to tackle this.
function groupPasses(passArray, array2) {
// Create an array of the intervals from array2
let intervals = [];
for(let i = 0; i < array2.length; i += 2){
if(array2[i] && array2[i+1]){
intervals.push({
start: new Date(array2[i].createdAt),
end: new Date(array2[i+1].createdAt)
});
}
}
// Use reduce to create an object where the keys are the createdAt dates
// and the values are arrays of elements from passArray that fall within the interval
return passArray.reduce((acc, pass) => {
let passDate = new Date(pass.createdAt);
// Use filter to find the interval that the pass falls in
let interval = intervals.filter((interval) =>
passDate >= interval.start && passDate <= interval.end
)[0];
if(interval){
let key = interval.start.toISOString();
if(!acc[key]){
acc[key] = [];
}
acc[key].push(pass);
}
return acc;
}, {});
}
This function first creates an array of intervals from array2
. Then it iterates over passArray
using reduce. For each element in passArray
, it uses filter
to find the interval from array2
that the element's createdAt
date falls within. It then adds the element to the appropriate array in the object it's creating.
Remember to ensure that array2
is sorted in ascending order by the createdAt
property before using this function. This function assumes that every 'opened' operation in array2
is followed by a 'closed' operation.
The final object created by this function will have keys that are the start dates of the intervals in array2
, and the values are arrays of elements from passArray
that fall within those intervals. You can get the number of passes in each interval by getting the length of these arrays.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论