Having issues with filtering an array within my JSON file.
Filtering the main array works fine, but I'm struggling to filter subarrays.
This is the structure of my JSON file:
[
{
"id": 1354,
"name": "name",
"type": "simple",
"status": "ok",
"categories": [
{
"id": 79,
"name": "A",
},
{
"id": 68,
"name": "B",
}
]
},
{
"id": 1368,
"name": "name",
"type": "simple",
"status": "ok",
"categories": [
{
"id": 79,
"name": "A",
},
{
"id": 72,
"name": "C",
}
]
},
{...}
]
The current code successfully filters and returns items with status: 'ok'
:
return items.filter( item => {
return item.status == 'ok';
});
However, I'm looking to filter and return items with category.name: 'B'
.
The code I have only seems to work with the first category, not considering subsequent categories:
return items.filter( item => {
for (let category of item.categories) {
return category.name == 'B';
}
});
Open to any suggestions or ideas, thank you.