Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
filtrar en matriz según la fecha más reciente
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[];

Arriba hay datos de muestra. Necesito implementar un caso similar. Mi implementación es similar a

este.

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

Resultado actual:

searchTerm: AB Entonces necesito obtener una dirección cuyo nombre comience con 'AB'

Resultado:

 [ 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' } ]

Resultado esperado: necesito eliminar la matriz duplicada según la ciudad pero la última fecha actualizada

es decir, la única ciudad con Pune con 'actualizado:' 12 de marzo de 2012 a las 12:00:00 a.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede usar Array.reduce para crear un mapa de desarrolladores, usando addr.city como clave.

Solo reemplazaremos cualquier desarrollador para una ciudad determinada si su propiedad updated es más nueva que la existente:

 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 Report

0

Array.reduce hará el truco por ti.

 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 Report

0

He comentado cada paso para entender lo que estamos haciendo.

por favor mire mi solución

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!