I have a nest list of Objects in JavaScript and I want to filter them using a 'search string' and a property value.
I want to only collect the Categories that have Children who are not Hidden and contain at least one profile with the search keyword in it's name. I'm not quite sure how to write the function itself, utilizing the bells and whistles of JavaScript.
Below is pseudo code
initial list:
categories = [
{
"category":"Players",
"children":[
{
"profiles":[
{
"name":"Kevin"
},
{
"name":"Kevin Young"
},
{
"name":"Kevin Old"
}
],
"isHidden":false
},
{
"profiles":[
{
"name":"Mike"
},
{
"name":"Mike Baby"
}
],
"isHidden":false
},
{
"profiles":[
{
"name":"Joe Old"
}
],
"isHidden":false
}
]
},
{
"category":"Teams",
"children":[
{
"profiles":[
{
"name":"Cowboys"
}
],
"isHidden":true
},
{
"profiles":[
{
"name":"Steelers"
}
],
"isHidden":false
}
]
}
]
pseudo function:
filterList: function(): Categories[] {
return categories.filter((cat: Category): boolean => {
// loop through each categories list of children
// if child is hidden skip the child item
// if child's profiles do not contain the word 'old' (Caseinsensitive) skip child
// lastly do not return a category in the return if there are no children
// after filtering them based on the conditions above
// if category contains one or more children after children
});
}
results of function:
categories = [
{
"category":"Players",
"children":[
{
"profiles":[
{
"name":"Kevin"
},
{
"name":"Kevin Young"
},
{
"name":"Kevin Old"
}
],
"isHidden":false
},
{
"profiles":[
{
"name":"Joe Old"
}
],
"isHidden":false
}
]
}
]