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

162
Vistas
filter in array based on latestdate
Sample Data

var devs = [
    {
        name: 'ABC',
        age: 26,

        addr:{
            country:'India',
            city:'Pune',
            updated:'Mar 12 2012 10:00:00 AM'   //This is Date Proprty
        }
    },
    {
        name: 'ABD',
        age: 25,
      
        addr:{
            country:'India',
            city:'Pune',
            updated:'Mar 12 2012 12:00:00 AM'
        }
    },
    {
        name: 'ABI',
        age: 27,
      
        addr:{
            country:'UK',
            city:'London',
            updated:'Mar 13 2012 12:00:00 AM'
        }
    }
. ...Other
]


export interface IAddress{
    id?: string;
    country?:string;
    city?:string;
    updated?:Date;
}

let address:IAddress[];

Above is sample data. I need to implement similar case . My implementation is similar to

this.

let address= devs.filter(temp => temp.name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1).map(({addr}) => ({addr}));

Current Result:

searchTerm: AB Then in need to get Address whos name start with 'AB'

Result:

[
  
     addr:{
            country:'India',
            city:'Pune',
            updated:'Mar 12 2012 10:00:00 AM'
        },   ///this to be remove in the final result bacause City Pune allreay available

     addr:{
            country:'India',
            city:'Pune',
            updated:'Mar 12 2012 12:00:00 AM'
        },
    
      ddr:{
            country:'UK',
            city:'London',
          updated:'Mar 13 2012 12:00:00 AM'
        }
   
]

Expected Result: I nee to Remove the duplicate array based on city but latest updated date

i.e only city with Pune with 'updated:'Mar 12 2012 12:00:00 AM' only should be take in the final resullt..Please some one help me to sorr and filter latest record

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

0

You can use Array.reduce to create a map of devs, using the addr.city as the key.

We'll only replace any dev for a given city if their updated property is newer than the existing one:

var devs = [ { name: 'ABC', age: 26,  addr:{ country:'India', city:'Pune', updated:'Mar 12 2012 10:00:00 AM' } }, { name: 'ABD', age: 25,  addr:{ country:'India', city:'Pune', updated:'Mar 12 2012 12:00:00 AM' } }, { name: 'ABI', age: 27,  addr:{ country:'UK', city:'London', updated:'Mar 13 2012 12:00:00 AM' } } ]; 

// Update our map if 
// a) the entry doesn't exist or 
// b) the updated date is later than the most recent one.

const addresses = Object.values(devs.reduce((acc, cur) => {
   if (!acc[cur.addr.city] ||
       Date.parse(cur.addr.updated) < Date.parse(acc[cur.addr.city].addr.updated)) { 
       acc[cur.addr.city] = { addr: cur.addr };
   }
   return acc;
}, {}))

console.log('Addresses:', addresses)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Array.reduce will do the trick for you.

const searchTerm = "AB";
var devs = [
  { name: 'ABC', age: 26, addr: { country: 'India', city: 'Pune', updated: 'Mar 12 2012 10:00:00' } },
  { name: 'ABD', age: 25, addr: { country: 'India', city: 'Pune', updated: 'Mar 12 2012 12:00:00' } },
  { name: 'ABI', age: 27, addr: { country: 'UK', city: 'London', updated: 'Mar 13 2012 12:00:00' } }
];
let address= devs.filter(temp => temp.name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1).reduce((acc, curr) => {
  // Check if a node exist in accumulator with same city
  const index = acc.findIndex((item) => item.addr.city === curr.addr.city);
  if(index > -1) {
    // If exist check fro the latest time
    const existingTime = new Date(acc[index].addr.updated).getTime();
    const newTime = new Date(curr.addr.updated).getTime();
    // Replace the node with latest time
    if(newTime > existingTime) {
      acc[index] = curr;
    }
  } else {
    acc.push(curr);
  }
  return acc;
}, []);
console.log(address);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I have commented each step yo understand what we are doing

please look into my solution

var devs = [
    {
        name: 'ABC',
        age: 26,

        addr:{
            country:'India',
            city:'Pune',
            updated:'Mar 12 2012 10:00:00 AM'   //This is Date Proprty
        }
    },
    {
        name: 'ABD',
        age: 25,
      
        addr:{
            country:'India',
            city:'Pune',
            updated:'Mar 12 2012 12:00:00 AM'
        }
    },
    {
        name: 'ABI',
        age: 27,
      
        addr:{
            country:'UK',
            city:'London',
            updated:'Mar 13 2012 12:00:00 AM'
        }
    }
]

const result = devs
.map(i => i.addr) // show only addreses
.reduce((acc, curr) => {
const existed = acc.find(i => i.city === curr.city) // check if same item already exist
    if(!existed) {
    return [...acc, curr]; // if no exist - add current item
  } else {
    // Same item already stored
    if(new Date(existed.updated) < new Date(curr.updated)) { // need to check which item is newest
        return acc // newest already stored
    } else {
    const existedIndex = acc.findIndex(i => i.city === curr.city)
        acc.splice(existedIndex, 1, curr)// need yo replace oldest with newwest
      return acc; // return updated array
    }
  }
}, [])
console.log(result)

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