简体   繁体   English

删除数组中以前的相似条目

[英]de-dupe previous similar entries in an array

Let's say I have the following array of objects:假设我有以下对象数组:

[
    {
        status: 'Draft',
        order: 1
    },
    {
        status: 'Accepted',
        order: 2
    },
    {
        status: 'Ordered',
        order: 3
    },
    {
        status: 'Ordered',
        order: 4
    },
    {
        status: 'Sent',
        order: 5
    }
]

I was wondering, I would like to de-dupe objects within this array, but only de-dupe the "oldest" object with a duplicated status, that is, for the above array, I would like to end up with the following de-duped array:我想知道,我想对这个数组中的对象进行重复数据删除,但只对具有重复状态的“最旧”object 进行重复数据删除,也就是说,对于上面的数组,我想以以下去重复结束 -欺骗数组:

[
    {
        status: 'Draft',
        order: 1
    },
    {
        status: 'Accepted',
        order: 2
    },
    {
        status: 'Ordered',
        order: 4
    },
    {
        status: 'Sent',
        order: 5
    }
]

That is, status Ordered 4 is "younger" than status Ordered 3... and not simply to just de-dupe...this could extended to the following:也就是说,状态 Ordered 4 比状态 Ordered 3“更年轻”……而且不仅仅是去重复……这可以扩展到以下内容:

[
    {
        status: 'Draft',
        order: 1
    },
    {
        status: 'Accepted',
        order: 2
    },
    {
        status: 'Ordered',
        order: 3
    },
    {
        status: 'Ordered',
        order: 4
    },
    {
        status: 'Ordered',
        order: 5
    },
    {
        status: 'Confirmed',
        order: 6
    }
]

will result in:将导致:

[
    {
        status: 'Draft',
        order: 1
    },
    {
        status: 'Accepted',
        order: 2
    },
    {
        status: 'Ordered',
        order: 5
    },
    {
        status: 'Confirmed',
        order: 6
    }
]

NB There is one caveat, I don't actually have the order key in the object, this is purely for demonstrative purposes to demonstrate what I mean for the purposes of this question.. NB有一个警告,我实际上没有 object 中的订单密钥,这纯粹是为了演示目的,以证明我对这个问题的意思。

Pretty simple reduce function. Basically we create an object (which can only have one value per key).非常简单减少 function。基本上我们创建一个 object(每个键只能有一个值)。 The "last" entry in your array with a specific status will be preserved.数组中具有特定状态的“最后”条目将被保留。

 const input = [ { status: 'Draft', order: 1 }, { status: 'Accepted', order: 2 }, { status: 'Ordered', order: 3 }, { status: 'Ordered', order: 4 }, { status: 'Ordered', order: 5 }, { status: 'Confirmed', order: 6 } ]; const output = Object.values(input.reduce((a, e) => { a[e.status] = e; return a; }, {})); console.log(output);

A one-liner with a Map:带有 Map 的单线:

 const arr = [ { status: 'Draft', order: 1 }, { status: 'Accepted', order: 2 }, { status: 'Ordered', order: 3 }, { status: 'Ordered', order: 4 }, { status: 'Ordered', order: 5 }, { status: 'Confirmed', order: 6 } ]; const deduped = [...new Map(arr.map(o => [o.status, o])).values()]; console.log(deduped);

JS doesn't include a dedup/distinct function natively so you have to make your own such as by using a filter to check for duplicates JS 本身不包含 dedup/distinct function,因此您必须自己制作,例如使用过滤器检查重复项

the standard call back for the filter is (item, index, array)过滤器的标准回调是(item, index, array)

this means that you can do这意味着你可以做

filter((item,idx,arr)=> arr.findIndex(other=>areEqual(item,other)) != idx)

this reads as remove all elements where the they have previously existed in the array because findIndex returns the location of the first match, so if they current index doesn't equal it then its not the first match这读作删除它们以前存在于数组中的所有元素,因为 findIndex 返回第一个匹配项的位置,所以如果它们当前索引不等于它那么它不是第一个匹配项

the reason for findindex rather than indexOf is that findIndex allows you to specify how to check for equality使用 findindex 而不是 indexOf 的原因是 findIndex 允许您指定如何检查是否相等

this is where the areEqual callback comes in, it takes 2 items and returns true if they are equal这是 areEqual 回调进来的地方,它需要 2 个项目,如果它们相等则返回 true

so could be所以可能是

areEqual(a,b){
    return a.field === b.field
}

of course this is overkill for just a.field === b.field and could be rewritten as当然,这对于 a.field === b.field 来说太过分了,可以重写为

filter((item,idx,arr)=> arr.findIndex(other=>item.field === other.field) != idx)

but you indicate that your test for equality might be more complex than a single known field so the callback to areEqual allows much more complex logic但是您指出您的相等性测试可能比单个已知字段更复杂,因此对 areEqual 的回调允许更复杂的逻辑

unfortunately there isn't a findLastIndex but simply reversing the array before filtering will fix that, then reverse the result to regain the original order不幸的是没有 findLastIndex 但在过滤之前简单地反转数组将解决这个问题,然后反转结果以重新获得原始顺序

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

相关问题 有没有一种方法可以对JavaScript数组进行重复数据删除并合并数据值? - Is there a way to de-dupe a javascript array and combine values of the data? 使用Array.prototype.filter()不会对数组重复数据删除 - Using Array.prototype.filter() doesn't de-dupe array of arrays 如何对数组进行重复数据删除,并根据其重复向数组的每个 object 添加其他属性 - How can I de-dupe an array, and add additional properties to each object of the array based on its duplications 推送到阵列会覆盖相似的条目 - Pushing to Array Overwrites Similar Entries 根据 Javascript 对象的属性值过滤和去重复对象 - Filter and de dupe Javascript Objects based on it's property value Javascript数组解构和Object.entries - Javascript array de-structuring and Object.entries 对象数组不断覆盖先前的条目 - Array of objects keeps overwriting previous entries 如何检查数组中的相似条目,并根据相似条目使用javascript合并值? - How can i check similar entries in an array and according to similar entries merge the values using javascript? 使用 .reduce() 使用先前数组中的所有相似键值创建一个新数组 - Make a new array with all the similar key values from previous array using .reduce() 清除以前拖动的文件条目? - Clearing previous dragged file entries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM