I have a Vue project and need to search an array with nested objects for a specific object and then return it. The user has a text input field for searching and the search should target "title".
The data looks like this:
const data =
[{
"catId": "1",
"catTitle": "a",
"exampleArray": [{
"id": "111",
"title": "aaa"
}, {
"id": "222",
"title": "bbb"
}, {
"id": "333",
"title": "ccc"
}]
}, {
"catId": "2",
"catTitle": "b",
"exampleArray": [{
"id": "444",
"title": "ddd"
}, {
"id": "555",
"title": "eee"
}]
}, {
"catId": "3",
"catTitle": "c",
"exampleArray": []
}, {
"catId": "4",
"catTitle": "d",
"exampleArray": [{
"id": "555",
"title": "fff"
}]
}]
I have tried:
return data.filter(item => {
return item.catArray.filter(category=> {
return category.title.toLowerCase().includes(this.search.toLowerCase())
})
})
e.g. if user input is "aaa", should return:
[{
"catId": "1",
"catTitle": "a",
"exampleArray": [{
"id": "111",
"title": "aaa"
}]
}]
The search should also return all matching results.