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

229
Views
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 answers
Answer question

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 Report

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 Report

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