I have a dataset that resembles the structure of IconItems:
{
title: "Category title",
description: "Example description",
lists: [
{
id: "popular",
title: "Popular",
items: [
{
id: 1,
title: "title",
provider: "provider",
image: ""
},
....
]
},
{
id: "new",
title: "New",
items: [
{
id: 4,
title: "studioss2",
provider: "provider",
image: ""
},
....
]
}
I am looking to filter the nested array of items (IconItem type, singular) based on a string value.
For this purpose, I have defined two values:
const filterKeyOpt: Array<keyof IconItem> = ["title", "provider", "image"];
const searchQuery = 'sTudioSS'
I attempted to filter the data with the following function
const logData = () => {
let filteredData = data?.lists.map((item) => item.items.filter((i) => {
return filterKeyOpt.some((key) => i[key].toString().toLowerCase().includes(searchQuery.toLowerCase()))
} /// returns IconItem[][] | undefined but I want to return type IIconItems | undefined
))
console.log(filteredData)
}
However, the output is in the form of
IconItem[][]
I desire the entire object/type. Meaning, filtering based on a nested value and returning a copy of the data object, including matching properties. How can I achieve that?
Playground link