I have an array of objects like below,
[
{
id: 1,
holiday_for: 1,
holiday_on: "2021-10-26"
},
{
id: 2,
holiday_for: 3,
holiday_on: "2021-10-26"
},
{
id: 3,
holiday_for: 1,
holiday_on: "2021-11-26"
},
{
id: 4,
holiday_for: 3,
holiday_on: "2021-11-26"
},
{
id: 5,
holiday_for: 4,
holiday_on: "2021-11-26"
}
]
where the objects have the same dates but different holiday_for. holiday_for has 3 types 1, 3, 4. I am trying to find an object with the same dates which has max holiday_for.
If the array has 2 objects with the same dates and types 1 and 3 then 3 will get high priority. If it has 3 objects with the same dates with types 1, 3, 4 then 4 will get priority.
I have tried using reduce but it returns only the object with the highest type -
const filtered = holidays.reduce((max, cur) =>
max.holiday_for > cur.holiday_for ? max : cur
)
Output type
[
{
id: 2,
holiday_for: 3,
holiday_on: "2021-10-26"
},
{
id: 5,
holiday_for: 4,
holiday_on: "2021-11-26"
}
]