繁体   English   中英

按对象属性删除对象数组的 doublon

[英]Remove doublon for an array of objects by object property

我有一个像这样的对象数组(按时间戳升序排序):

[
 {id: 'o3134432S2', tot: '45', type: 'dynamic', timestamp: '2018-04-03'},
 {id: '3566543aa3', tot: '34', type: 'saved', timestamp: '2018-04-03'},
 {id: '4530134a97', tot: '34', type: 'gold', timestamp: '2018-04-04'},
 {id: '234554b333', tot: '42', type: 'saved', timestamp: '2018-04-04'},
 {id: '2463545633', tot: '55', type: 'dynamic', timestamp: '2018-04-05'},
 {id: '5654324566', tot: '13', type: 'saved', timestamp: '2018-04-06'}
]

这个数组可能真的很长(超过一千个值),每天可能有 1 个金币、1 个储蓄,或两者兼而有之。 我想每天删除达布隆,如果存在的话,优先考虑保存的达布隆。 就我而言,这将返回:

[
 {id: '3566543aa3', tot: '34', type: 'saved', timestamp: '2018-04-03'},
 {id: '234554b333', tot: '42', type: 'saved', timestamp: '2018-04-04'},
 {id: '2463545633', tot: '55', type: 'dynamic', timestamp: '2018-04-05'},
 {id: '5654324566', tot: '13', type: 'saved', timestamp: '2018-04-06'}
]

我已经使用很多 while 和 foreach 做了一些事情,但它需要 5 到 30 秒,而且我很确定它可以更快。

    function remouveDoublon(originalArray) {
    const filteredSaved = originalArray.filter(forecast => {forecast.type === "saved" })

    const filteredDynamic = forecastProductOrders.filter((forecast) => { return forecast.type === 'dynamic' })
    let exists = false;
    if (filteredSaved.length > 0) {
        filteredSaved.forEach(forecast => {
            exists = false;
            for (var i = 0; i < filteredDynamic.length; i++) {
                if (moment(filteredDynamic[i].timestamp).isSame(moment(forecast.timestamp), 'day') && filteredDynamic[i].supplierProductId === forecast.supplierProductId) {
                    filteredDynamic[i].tot = forecast.tot;
                    exists = true;
                    break;
                }
            }
            if (exists === false) {
                let objToPush = forecast;
                objToPush.type === 'saved';
                filteredDynamic.push(objToPush);
            }
        })
    }
    return filteredDynamic
}

有没有人知道如何做这个?

非常感谢

您可以遍历数组并使用日期作为键将每个条目保存在字典中。 如果条目已经存在,请检查类型并仅在“已保存”时更新它。 然后再次将字典转换为列表。 这将具有线性运行时间。 也许像这样

unique = {}
for entry in entries:
    if unique[entry.date] and unique[entry.date].type == "saved":
         continue
    unique[entry.date] = entry

print(list(unique))

您可以使用此方法,然后根据需要再次将其排序为我的时间戳。

 const data = [ {id: 'o3134432S2', tot: '45', type: 'dynamic', timestamp: '2018-04-03'}, {id: '3566543aa3', tot: '34', type: 'saved', timestamp: '2018-04-03'}, {id: '4530134a97', tot: '34', type: 'gold', timestamp: '2018-04-04'}, {id: '234554b333', tot: '42', type: 'saved', timestamp: '2018-04-04'}, {id: '2463545633', tot: '55', type: 'dynamic', timestamp: '2018-04-05'}, {id: '5654324566', tot: '13', type: 'saved', timestamp: '2018-04-06'} ] let result = data.filter(o => o.type === 'saved') data.filter(o => o.type !== 'saved').forEach(g => { if (result.filter(r => r.timestamp === g.timestamp).length === 0) result.push(g) }) console.log(result)

使用Array#Reduce 使用时间戳过滤数组。然后如果在新数组比较类型中存在相同的时间戳。然后只添加保存的类型对象

 var arr = [ {id: 'o3134432S2', tot: '45', type: 'gold', timestamp: '2018-04-03'}, {id: '3566543aa3', tot: '34', type: 'saved', timestamp: '2018-04-03'}, {id: '4530134a97', tot: '34', type: 'gold', timestamp: '2018-04-04'}, {id: '234554b333', tot: '42', type: 'saved', timestamp: '2018-04-04'}, {id: '2463545633', tot: '55', type: 'gold', timestamp: '2018-04-05'}, {id: '5654324566', tot: '13', type: 'saved', timestamp: '2018-04-06'} ]; arr = arr.reduce(function(a,b){ var ind = a.map(i=>i.timestamp).indexOf(b.timestamp); if(ind == -1){ a.push(b); }else{ a[ind] = a[ind]['type'] == 'saved'? a[ind]:b; } return a; },[]) console.log(arr)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM