Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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:

[
    {
        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
    }
]

N.B. 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..

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!