【发布时间】:2021-07-15 14:03:23
【问题描述】:
我正在尝试从包含唯一 id(猫鼬对象 id)和字符串数组的多个对象数组中查找公共数据。
例如:
array1 = [{
_id: "60f027f98b55eb2df1f36c04",
date: "2021-07-15T12:18:12.223Z",
time: "30",
hours: ["8:00 AM", "8:30 AM"]
}]
array2 = [{
_id: "60f027f98b55eb2df1f36c05",
date: "2021-07-15T12:18:12.223Z",
time: "60",
hours: ["7:30 AM", "8:30 AM", "9:30AM"]
}]
array3 = [{
_id: "60f027f98b55eb2df1f36c06",
date: "2021-07-16T12:12:12.223Z",
time: "30",
hours: ["7:00 AM", "8:30 AM"]
}]
输出在数组中应该有最大公共日期的最大公共值,并且该日期应该有最大公共小时。
所以示例输出应该如下所示。
common_data = {
date: "2021-07-15T12:18:12.223Z",
time: "30",
hours: "8:30AM"
}
我查看了其他答案并尝试了以下方法: 合并所有数组和
let result = merged_slots_array.shift().filter(function(v) {
return merged_slots_array.every(function(a) {
const matchDate = a.date === v.date;
const getMatchTime = a.hours.shift().filter(function(x) {
return v.hours.every(function(t) {
return x.indexOf(t) !== -1;
})
});
return matchDate && getMatchTime
});
});
但出现错误merged_slots_array.shift(...).filter is not a function
【问题讨论】:
标签: javascript arrays ecmascript-6