【问题标题】:Get common data from multiple arrays of object that containing a unique id and nested array using javascript es6使用javascript es6从包含唯一ID和嵌套数组的多个对象数组中获取公共数据
【发布时间】: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


    【解决方案1】:

    连接数组后,可以通过只保留重复项的过滤器找到最大公共时间,然后对其进行排序。一旦我们有了它,我们就可以查询每个数组以确保它包含最大小时数,然后提取最大日期和时间。我的输出与你的略有不同,因为我过滤了最大时间、小时和日期

    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"]
    }]
    
    const getCommon = (arrays) => {
    let group = [].concat.apply([], [...arrays])
    let hour = group.map(e=>e.hours).flat().filter((e,i,a) => a.indexOf(e) !== i).sort((a,b) => a.localeCompare(b))[0]
    let common = group.filter(e=>e.hours.includes(hour))
    let time = Math.max(...common.map(e => +e.time))
    let date = common.map(e => e.date).sort((a,b) => new Date(b) - new Date(a))[0];
    return {date: date, time: time, hours: [hour]}
    }
    let arg = []
    arg.push(array1)
    arg.push(array2)
    arg.push(array3)
    console.log(getCommon(arg))

    TS Playground

    【讨论】:

    • 在打字稿The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type在线let date = common.map(e => e.date).sort((a,b) => new Date(b) - new Date(a))[0];中出现此错误
    • 只需将其设置为 type:any。看看我添加到答案中的 ts playground 链接。
    • 我正在尝试将数组放入循环中,因此使用 merged_slots_array.push(slots); 然后执行 let found = this.getCommon(merged_slots_array) 但未定义。 { date: undefined, time: -Infinity, hours: [ undefined ] } 作为返回值;
    • 我更新了我的答案以显示 3 次推送,但实际上它并没有改变任何东西。我不认为您可以共享指向工作示例的链接吗?或者你能告诉我你的merged_slots_array 输入getCommon 时的样子吗?
    • 我得到了一个共同的日期作为回报,它甚至不在数组中。怎么样?
    猜你喜欢
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多