I have an array of objects:
[{
id: 1,
cod: '123',
val: true
},
{
id: 2,
cod: '123',
val: true
},
{
id: 3,
cod: '123',
val: true
},
{
id: 4,
cod: '456',
val: true
},
{
id: 5,
cod: '456',
val: true
}]
I need to maintain the val property true for the objects which have the same cod and have the higher id. For example, I'd need the following output:
[{
id: 1,
cod: '123',
val: false
},
{
id: 2,
cod: '123',
val: false
},
{
id: 3,
cod: '123',
val: true
},
{
id: 4,
cod: '456',
val: false
},
{
id: 5,
cod: '456',
val: true
}]
How could I change this array in order to obtain the desired results? Should I use filter? I'm a little lost.