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

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

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 Denunciar

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 Denunciar

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