JavaScript
- 5 ответов
- 0 вопросов
1
Вклад в тег
const getYear = (value) => {
const timeStmp = Date.parse(value);
return new Date(timeStmp).getUTCFullYear();
}
const monthDiff = (dateFrom) => {
const baseYear = new Date(dateFrom).getUTCFullYear();
const baseDate = new Date(baseYear, 11);
const bday = new Date(dateFrom);
return new Date(baseDate).getMonth() - bday.getMonth() +
(12 * (new Date(baseDate).getFullYear() - bday.getFullYear()));
}
const cowsFemale = cows
.filter(res => res.gender === 'Ұрғашы')
.reduce((accum, curVal) => {
const currentYearBorn = getYear(curVal.bdate);
const monthsAgo = monthDiff(curVal.bdate);
const younger = monthsAgo <= 20;
if (younger) {
if (!accum[currentYearBorn]) {
accum[currentYearBorn] = {
count: 1
}
} else {
accum[currentYearBorn] = {
count: accum[currentYearBorn].count + 1
}
}
}
return accum
}, {})
{
2019: {count: 11}
2020: {count: 9}
}