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

78
Visualizações
Take unique and new values ​from the array with duplicate data

Hi guys I have this scenario:

const data = [
    {
       "name": "Hebert",
       "timestamp": 1640371815, // 15:50:15
    },{
       "name": "Brien",
       "timestamp": 1640373855, // 16:24:15
    },{
       "name": "Hebert",
       "timestamp": 1640363601 // 13:33:21 
    },{
       "name": "Mary",
       "timestamp": 1640356701 // 11:38:21
    },{
       "name": "Mary",
       "timestamp": 1640356521 // 11:35:21 
    },{
       "name": "Mary",
       "timestamp": 1640356401 // 11:33:21
   }
];

I need to get lasted one from duplicates by timestamp:

   // expected result
    [
       {
         "name": "Hebert",
         "timestamp": 1640371815 // 15:50:15 newest one
       },{
         "name": "Brien",
         "timestamp": 1640373855 // 16:24:15 unique
       },{
         "name": "Mary",
         "timestamp": 1640356701 // 11:38:21 newest
       }
    ];

I try this:

    data.sort((a,b) => a.name.localeCompare(b.name))
        .filter((chat, index, self) => self.findIndex( c => c.name == chat.name ) == index )

output duplicated removed but no considering newest timestamp between each duplicates

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Rather than sorting and using filter you can iterate the array just once storing items in a Map or object using the names as keys and then just update the lowest timestamp as you loop through each item.

Then finally convert the object values back to array

const items = new Map()

data.forEach(({name,timestamp}) => {  
  const o = items.get(name);
  if(o){
    timestamp < o.timestamp && o.timestamp = timestamp;    
  } else {
     items.set(name, {name, timestamp});
  }
});

const res = [...items.values()]

console.log(res)
<script>

const data = [
    {
       "name": "Hebert",
       "timestamp": 1640371815, // 15:50:15
    },{
       "name": "Brien",
       "timestamp": 1640373855, // 16:24:15
    },{
       "name": "Hebert",
       "timestamp": 1640363601 // 13:33:21 
    },{
       "name": "Mary",
       "timestamp": 1640356701 // 11:38:21
    },{
       "name": "Mary",
       "timestamp": 1640356521 // 11:35:21 
    },{
       "name": "Mary",
       "timestamp": 1640356401 // 11:33:21
   }
];
</script>

about 4 years ago · Santiago Trujillo Relatório

0

const data = [
    {
       "name": "Hebert",
       "timestamp": 1640371815, // 15:50:15
    },{
       "name": "Brien",
       "timestamp": 1640373855, // 16:24:15
    },{
       "name": "Hebert",
       "timestamp": 1640363601 // 13:33:21 
    },{
       "name": "Mary",
       "timestamp": 1640356701 // 11:38:21
    },{
       "name": "Mary",
       "timestamp": 1640356521 // 11:35:21 
    },{
       "name": "Mary",
       "timestamp": 1640356401 // 11:33:21
   }
];

const seen = {};
const dataModified = [];

data.forEach((elem) => {
  const index = seen[elem.name];
  
  if(index == undefined){
    dataModified.push(elem);
    seen[elem.name] = dataModified.length - 1;
  } else if(dataModified[index].timestamp < elem.timestamp){
    dataModified.splice(index, 1, elem);
  }
});

console.log(dataModified);

about 4 years ago · Santiago Trujillo Relatório

0

data.sort((a,b) => a.name.localeCompare(b.name))
.filter((chat, index, self) => self.findIndex(c => c.name === chat.name && c.timestamp > chat.timestamp)<0)

The idea is that findIndex would only return the index when there exists another duplicate with greater timestamp, else it'd return -1.

about 4 years ago · Santiago Trujillo 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