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

221
Vistas
How to group objects together and eliminate duplicates?

I have a requirement where I have data similar to this:

[
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-13 18:04:34.356251+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "E10003",
      "active": "A",
      "classCode": "EX002",
      "publishedTime": "2022-02-21 03:34:10.139843+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-23 12:43:00.08635+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "T",
      "classCode": "EX004",
      "publishedTime": "2022-02-24 12:00:00.08635+00"
    }
  }
]

I need to eliminate the duplicates for each positionCode. For example if there are multiple objects with the same positionCode, I need to retain only the latest one in the array and remove all the other duplicates from the array and get back the array in the same format with the duplicates removed.

Please suggest a way to do this in Javascript.

I thought of grouping the objects using the positionCode and then checking which is the latest. Not sure how to proceed and would appreciate help on this.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can achieve this by filtering positionCodes based on single item and compare their publishedTime if current item publishedTime is greather than the item which is already in our array (arr) or item which is unique to them. Then it will push to temporary array otherwise not.

let arr =[
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-13 18:04:34.356251+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "E10003",
      "active": "A",
      "classCode": "EX002",
      "publishedTime": "2022-02-21 03:34:10.139843+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "A",
      "classCode": "EX003",
      "publishedTime": "2022-02-23 12:43:00.08635+00"
    }
  },
  {
    "header": {
      "httpFittingRequestAttempts": 1
    },
    "payload": {
      "positionCode": "A10007",
      "active": "T",
      "classCode": "EX004",
      "publishedTime": "2022-02-24 12:00:00.08635+00"
    }
  }
]

let a=[]
arr.forEach((item,index)=>{
   let i=arr.filter(e=>e.payload.positionCode===item.payload.positionCode && new Date(e.payload.publishedTime)>new Date(item.payload.publishedTime));
   if(!i.length) a.push(item);
});
console.log(a) // here is your final result array

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