I am trying to return an array from a json object. Each object has some data and another nested array/object
like this:
{
"data": [
{
"name": "name",
"id": "id",
"amount": 4000,
"items": [
{
"number": 12,
"isAvailable": true,
},
{
"number": 15,
"isAvailable": true,
},
{
"number": 16,
"isAvailable": true,
},
{
"number": 17,
"isAvailable": true,
}
]
},
{
"name": "name2",
"id": "id2",
"amount": 3000,
"items": [
{
"number": 12,
"isAvailable": true,
},
{
"number": 15,
"isAvailable": true,
},
{
"number": 16,
"isAvailable": false,
},
{
"number": 17,
"isAvailable": true,
}
]
},
{
"name": "name4",
"id": "id4",
"amount": 1200,
"items": [
{
"number": 12,
"isAvailable": true,
},
{
"number": 15,
"isAvailable": true,
},
{
"number": 16,
"isAvailable": true,
},
{
"number": 17,
"isAvailable": true,
}
]
}
]
}
I want to check all objects and see if in any of these objects has a item that has "isAvailable": false
If an item is not available I need to remove the parent object from the array (so not the item array but the full object above that.
Then I would like to return the array where this object has been removed.
I got this to work by using ES6 findIndex
array.findIndex(object => object.items.some(item => !item.isAvailable));
Which will return the index of the parent array that has it, which I can use to splice the array. But this only works on a single object. If I have another object it will still return the first index.
How can I check if multiple objects exist based on the criteria and remove them from an array?
"number": 12".filterinstead of.findIndex