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:
[
{
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:
[
{
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..
Pretty simple reduce function. Basically we create an object (which can only have one value per key). 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:
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
the standard call back for the filter is (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
the reason for findindex rather than indexOf is that findIndex allows you to specify how to check for equality
this is where the areEqual callback comes in, it takes 2 items and returns true if they are equal
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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.