【发布时间】:2019-05-19 02:44:38
【问题描述】:
我有一个对象数组。根据下拉值的选择过滤对象数组。
const itemsList=[{
"id":"123",
"problems":{
"causes":[
{
"SSEnabled": true,
"IndusEnabled": false,
"LogEnabled": true
}
]
}
},
{
"id":"234",
"problems":{
"causes":[
{
"SSEnabled": false,
"IndusEnabled": false,
"LogEnabled": true
}
]
}
}]
我们有一个下拉菜单来过滤 SSEnabled 原因。下拉选项有“show”、“nofilter”、“exclude”。
需要根据下拉选择过滤列表。
如果选择了“SSEnabled”下拉菜单的“show”选项,则“SSEnabled”:“true”的列表项应该是结果。(即;id:“123”)
如果选择“SSEnabled”原因的“排除”,则“SSEnabled:false”应为结果的列表项。(即;id:“234”)
如果选择“nofilter”,它应该忽略过滤器。 (即;id:“123”,id:“234”)
filterList(filterType, filterValue, itemsList) {
// filterType : SSEnabled (type of dropdown changed)
//filterValue : show, exclude , no filter
itemsList.map((items) => {
if (
items &&
items.problems &&
items.problems.causes &&
items.problems.causes.length
) {
items.problems.causes.filter((cause) => {
if (cause[filterType] === true && filterValue === 'show') {
return true;
}
if (cause[filterType] === false && filterValue === 'exclude') {
return true;
}
});
}
});
console.log(itemsList, 'filtered List');
}
但是列表没有被过滤。请帮忙过滤。
【问题讨论】:
-
使用数组过滤方式
-
那么在你的
map方法中你没有返回任何东西 -
Array.map 是错误的工具。 Array.map 适用于您想要一个包含与输入数组一样多的元素的输出数组。
标签: javascript arrays json object filter