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

235
Vistas
How to find array of object if current value more closest to now date?

I have JSON file that contain year value in string in edu_end_year. I want to choose one if the data more than one in same emp_id when the edu_end_year is closest to date now.

here's the data:

educations = [
    {
        "emp_id": 1,
        "edu_degree": "SMA",
        "edu_end_year": 2011
    },
    {
        "emp_id": 1,
        "edu_degree": "S1",
        "edu_end_year": 2016
    },
    {
        "emp_id": 2,
        "edu_degree": "S1",
        "edu_end_year": 2016
    }
]

here's result what i expected :

[
    {
        "emp_id": 1,
        "edu_degree": "S1",
        "edu_end_year": 2016
    },
    {
        "emp_id": 2,
        "edu_degree": "S1",
        "edu_end_year": 2016
    }
]

I was trying using filter() and find(), but still no luck.

please let me know if you need more information to solve that problem if it's still not enough.

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

0

I don't know this code efficient to handle big data or not, but that's work to fix if emp_id is same.

suggestion use this code after pagination

educations = [
    {
        "emp_id": 1,
        "edu_degree": "SMA",
        "edu_end_year": 2011
    },
    {
        "emp_id": 1,
        "edu_degree": "S1",
        "edu_end_year": 2016
    },
    {
        "emp_id": 2,
        "edu_degree": "S1",
        "edu_end_year": 2016
    },
    {
        "emp_id": 2,
        "edu_degree": "S1",
        "edu_end_year": 2016
    },
    {
        "emp_id": 2,
        "edu_degree": "S1",
        "edu_end_year": 2016
    }
]

for (let i = educations.length -1; i > 0; i--){
    if(educations[i].emp_id === educations[i-1].emp_id){
        educations.splice(i, i)
  }
}
console.log(educations)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array.reduce

  • use reduce() to loop through each element
  • Check your final array (acc) has items by checking emp_id into list
  • If it's first item then insert into
  • If same emp_id exists then check which year is greater
  • Replace newer year record into final array

educations = [
    {
        "emp_id": 1,
        "edu_degree": "SMA",
        "edu_end_year": 2011
    },
    {
        "emp_id": 1,
        "edu_degree": "S1",
        "edu_end_year": 2016
    },
    {
        "emp_id": 2,
        "edu_degree": "S1",
        "edu_end_year": 2015
    },
    {
        "emp_id": 2,
        "edu_degree": "S13",
        "edu_end_year": 2017
    },
    {
        "emp_id": 2,
        "edu_degree": "S14",
        "edu_end_year": 2016
    }
]

finalArr = educations.reduce((acc, prev) => {
  if(!acc.find(x=>x.emp_id === prev.emp_id)){
    acc.push(prev)
  }
  else{
    let record = acc.find(x=>x.emp_id === prev.emp_id);
    if(prev.edu_end_year >= record.edu_end_year){
      let index = acc.findIndex(x=>x.emp_id === prev.emp_id);
      acc[index] = prev;
    }
  }
  
  return acc;
}, []);
console.log(finalArr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

This should do it:

const educations = [{"emp_id": 1,"edu_degree": "SMA","edu_end_year": 2011},
    {"emp_id": 1,"edu_degree": "S1","edu_end_year": 2016},
    {"emp_id": 3,"edu_degree": "S3","edu_end_year": 2014},
    {"emp_id": 2,"edu_degree": "S1","edu_end_year": 2016}];

const res=Object.values(educations.reduce((a,c)=>{
 if(!a[c.emp_id]||a[c.emp_id].edu_end_year<c.edu_end_year) a[c.emp_id]=c;
 return a;
},{}));
console.log(res);

I misunderstood your question at first and changed my answer accordingly now. The above snippet uses a single .reduce() loop to filter out the latest elements for each .emp_id.

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