I have below object.
const data = [
{
status: 1,
date: '2020-12-01',
},
{
status: 1,
date: '2020-11-01',
},
{
status: 2,
date: '2020-12-01',
},
{
status: 4,
date: '2020-12-01',
},
{
status: 5,
date: '2020-12-01',
}
]
I need to filter out records with status 4 and 5.
Also, need to have only latest record for status 1.
So the result would be like below.
const data = [
{
status: 1,
date: '2020-12-01',
},
{
status: 2,
date: '2020-12-01',
},
]
This is what I have tried.
data.filter(obj => [1, 2, 3].includes(obj.status))
.filter(obj => obj.status === 1)
.sort((a, b) => new Date(b.date) - new Date(a.date))
But here I am losing object with other status.
I could do by storing filtered result first and then sorting and picking up the latest record, and use something like below
const result = filteredResult.push(latestRecordWithStatusOne)
But, is it possible to achieve this using the same chaining?