Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中使用下拉选择值筛选列表_Javascript_Arrays_Json_Object_Filter - Fatal编程技术网

在javascript中使用下拉选择值筛选列表

在javascript中使用下拉选择值筛选列表,javascript,arrays,json,object,filter,Javascript,Arrays,Json,Object,Filter,我有一个对象数组。基于选择下拉值过滤对象数组 const itemsList=[{ "id":"123", "problems":{ "causes":[ { "SSEnabled": true, "IndusEnabled": false, "LogEnabled": true } ] } }, { "id":"234", "problems":{ "causes":[

我有一个对象数组。基于选择下拉值过滤对象数组

const itemsList=[{
"id":"123",
"problems":{
    "causes":[ 
         {
        "SSEnabled": true,
        "IndusEnabled": false,
        "LogEnabled": true
        }
    ]
}
},
{
"id":"234",
"problems":{
    "causes":[
          {
        "SSEnabled": false,
        "IndusEnabled": false,
        "LogEnabled": true
        }
    ]
}
}]
我们有一个下拉列表来筛选无法找到的原因。下拉菜单的选项有“显示”、“不过滤”、“排除”

需要根据下拉选择筛选列表

如果选择了“SSEnabled”下拉列表的“show”选项,则“SSEnabled”:“true”的列表项应为结果。(即id:“123”)

如果选择了“sEnabled”原因的“exclude”,则“sEnabled:false”中的列表项应为结果。(即;id:“234”)

如果选择了“nofilter”,则应忽略该过滤器。(即id:“123”,id:“234”)


但是这个列表并没有被过滤。请帮助筛选。

一个干净的方法是定义实现逻辑的函数的查找,然后将正确的函数传递给
some()
(或
find()
)。您可以在
字段
中传递它,如
显示
排除
,然后按
进行筛选,如
已启用

const itemsList=[{“id”:“123”,“问题”:{“原因”:[{“SSEnabled”:true,“IndusEnabled”:false,“LogEnabled”:true}}},{“id”:“234”,“问题”:{“原因”:[{“SSEnabled”:false,“IndusEnabled”:false,“LogEnabled”:true}]
//实现基于关键字的选择逻辑
//将返回某些原因与条件匹配的项目
常量过滤器=(字段,键)=>{
让选项={
show:(i)=>i[key]==true,
nofilter:(i)=>真,
排除:(i)=>i[key]==false
} 
返回选项[字段]
}
//使用上述功能进行过滤:
让SSEnabled=itemsList.filter(item=>item.problems.cause.some(filters('show','SSEnabled'))
console.log(已启用)
让SS_not_enabled=itemsList.filter(item=>item.problems.causes.some(filters('exclude','SSEnabled'))
console.log(未启用SS_)
让LogEnabled=itemsList.filter(item=>item.problems.cause.some(filters('show','SSEnabled'))

console.log(LogEnabled)
使用数组过滤器方法然后在
map
方法中您没有返回任何内容Array.map是错误的工具。Array.map适用于您希望输出数组的元素数与输入数组的元素数相同的情况。可在筛选器中启用的SSEnabled对象是否可以是动态的??因为我有更多这样的属性要过滤,比如“inducenabled”、“logisticsEnabled”。@user8599269,当然不是简单的对象查找,而是将其作为一个函数,以获取您正在查找的属性。请参见编辑。
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');
}