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.
CodePudding user response:
One way to solve this would be to use a Map
and store the education with the highest edu_end_year
for each emp_id
.
const maxEducations = new Map();
for (const education of educations) {
const maxEducation = maxEducations.get(education.emp_id);
if (maxEducation && education.edu_end_year <= maxEducation.edu_end_year) continue;
maxEducations.set(education.emp_id, education);
}
The if-statment will skip the rest of the iteration if maxEducation
is found and the current education.edu_end_year
less or equal to maxEducation.edu_end_year.
If either maxEducation
is not set, or we encounter a new max edu_end_year
then the current education is set as the new maximum.
Once you've iterated through all educations and you'd like to have an array of the values, you can do the following
const result = Array.from(maxEducations.values());
const 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
}
];
const maxEducations = new Map();
for (const education of educations) {
const maxEducation = maxEducations.get(education.emp_id);
if (maxEducation && education.edu_end_year <= maxEducation.edu_end_year) continue;
maxEducations.set(education.emp_id, education);
}
console.log(Array.from(maxEducations.values()));
CodePudding user response:
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)
CodePudding user response:
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)
CodePudding user response:
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 cyear=new Date().getFullYear(),
year=educations.reduce((a,c)=>c.edu_end_year > a.edu_end_year ? c: a).edu_end_year,
res=educations.filter(d=>d.edu_end_year==year)
console.log(res);
In the .reduce()
loop I first calculate the closest day to today and store it in year
. This is assuming that all the dates are in the past. That way I don't even need to know the current date. In the following .filter()
loop I then extract only those elements with d.edu_end_year==year
.