I have an array of objects.
Within each object of the array, there can be multiple "Book" objects, all with dynamic keys. I want the objects with at least one "Book" object that is new.
For example:
const arr = [
{
id: '123',
book1242: {isNew: true},
book9023: {isNew: false},
},
{
id: '123',
book0374: {isNew: false},
book9423: {isNew: false},
},
{
id: '123',
book8423: {isNew: false},
book9023: {isNew: false},
},
{
id: '123',
book6534: {isNew: true},
book9313: {isNew: false},
},
]
So my filtered array will consist of the first and last element of the original array
Expected filtered array
const arr = [
{
id: '123',
book1242: {isNew: true},
book9023: {isNew: false},
},
{
id: '123',
book6534: {isNew: true},
book9313: {isNew: false},
},
]
I have tried using filter and map, but I get to the point where I have to loop through and check which book is new and I'm not sure how to return that object within the filter.