I am currently working on a Vue project where I need to search through an array of nested objects to find a specific object based on the "title" property. The user interface includes a text input field for conducting the search operation.
The structure of the data is as follows:
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 attempted the following code snippet in my search function:
return data.filter(item => {
return item.exampleArray.filter(element=> {
return element.title.toLowerCase().includes(this.search.toLowerCase())
})
})
For example, if the user inputs "aaa", the expected output should be:
[{
"catId": "1",
"catTitle": "a",
"exampleArray": [{
"id": "111",
"title": "aaa"
}]
}]
The search functionality is designed to retrieve all matching results that meet the specified criteria.