【发布时间】:2021-05-09 04:00:06
【问题描述】:
我有一个 JavaScript 中的对象嵌套列表,我想使用“搜索字符串”和属性值过滤它们。
我只想收集具有非隐藏子项的类别,并且至少包含一个名称中包含搜索关键字的配置文件。我不太清楚如何利用 JavaScript 的花里胡哨来编写函数本身。
下面是伪代码
初始列表:
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
}
]
}
]
伪函数:
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
});
}
函数结果:
categories = [
{
"category":"Players",
"children":[
{
"profiles":[
{
"name":"Kevin"
},
{
"name":"Kevin Young"
},
{
"name":"Kevin Old"
}
],
"isHidden":false
},
{
"profiles":[
{
"name":"Joe Old"
}
],
"isHidden":false
}
]
}
]
【问题讨论】:
-
问题很清楚..
-
哦顺便说一句..检查答案:D
标签: javascript