Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda