I have an array like such
const arr = [
{
id: '123',
book: {isNew: true}
},
{
id: '123',
book: {isNew: false}
},
{
id: '123',
book: {isNew: false}
},
{
id: '123',
book: {isNew: true}
},
]
I am tring to filter the array to return only the objects where the book object has isNew as true.
I was attempting to something like this
arr.filter(obj => {
// Use a forEach to determine which book obj is new
}
However a forEach can't return any values.
EDIT
Seems like my original way of explaining was a bit lacking.
Within each obj of the array, there can be multiple "Book" objects. All with dynamic keys. I want the object 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