I am working with an array of objects that contain multiple "Book" objects with dynamic keys. My goal is to filter the objects in the array to only include those that have at least one new "Book" object.
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},
},
]
The filtered array should only consist of the first and last elements from 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 attempted using filter
and map
, but I am struggling on how to identify and return the objects with new books within the filter function.