Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda