Javascript JS过滤器在另一个过滤器内不工作

Javascript JS过滤器在另一个过滤器内不工作,javascript,filter,Javascript,Filter,我需要得到过滤标签。只有那些字段来自“堆栈”列表的字段。 所以我需要先过滤“fields”数组,若并没有,那个么排除col、row或tab var tabs = [ { "name": "Main Tab", "layout": { "label": "side", "rows": [ [ { "col": 12, "fields": ["name"] } ], [ { "col": 12, "fields": ["st

我需要得到过滤标签。只有那些字段来自“堆栈”列表的字段。 所以我需要先过滤“fields”数组,若并没有,那个么排除col、row或tab

var tabs = [
  {
    "name": "Main Tab",
    "layout": {
      "label": "side",
      "rows": [
        [ { "col": 12, "fields": ["name"] } ],
        [ { "col": 12, "fields": ["status"] } ],
        [ { "col": 12, "fields": ["model"] } ],
        [ { "col": 12, "fields": ["office"] } ],
        [ { "col": 12, "fields": ["room"] } ]
      ]
    }
  },{
    "name": "Second Tab",
    "layout": {
      "label": "side",
      "rows": [
        [ { "col": 12, "fields": ["type"] } ],
        [ { "col": 12, "fields": ["ip"] } ]
      ]
    }
  },{
    "name": "Last Tab",
    "layout": {
      "label": "side",
      "rows": [
        [ { "col": 12, "fields": ["office"] } ],
        [ { "col": 12, "fields": ["location"] } ],
        [ { "col": 12, "fields": ["address"] } ]
      ]
    }
  }
]

var stack = ["location","address","room","office"]
但它不起作用。一点也不明白

试着这样做:

var filtered_tabs = tabs.filter(tab=>tab.layout.rows.filter(row=>row.filter(obj=>obj.fields.filter(field=>stack.indexOf(field)>-1).length>0).length>0).length>0)
预期结果如下:

var filtered_tabs = [
  {
    "name": "Main Tab",
    "layout": {
      "label": "side",
      "rows": [
        [ { "col": 12, "fields": ["office"] } ],
        [ { "col": 12, "fields": ["room"] } ]
      ]
    }
  },{
    "name": "Last Tab",
    "layout": {
      "label": "side",
      "rows": [
        [ { "col": 12, "fields": ["office"] } ],
        [ { "col": 12, "fields": ["location"] } ],
        [ { "col": 12, "fields": ["address"] } ]
      ]
    }
  }
]
但是我得到了相同的标签数组

试试这个:

var选项卡=[
{
名称:“主选项卡”,
布局:{
标签:“侧面”,
行:[
[{col:12,字段:[“name”]},
[{col:12,字段:[“status”]},
[{col:12,字段:[“model”]},
[{col:12,字段:[“office”]},
[{col:12,字段:[“room”]}]
]
}
},
{
名称:“第二个选项卡”,
布局:{
标签:“侧面”,
行:[{col:12,字段:[“type”]}],{col:12,字段:[“ip”]}]]
}
},
{
名称:“最后一个选项卡”,
布局:{
标签:“侧面”,
行:[
[{col:12,字段:[“office”]},
[{col:12,字段:[“位置”]},
[{col:12,字段:[“地址”]}]
]
}
}
];
var stack=[“位置”、“地址”、“房间”、“办公室”];
console.log(tabs.map(x=>(x.layout.rows=x.layout.rows.filter(row=>stack.includes(row[0].字段[0])),x))
.filter(x=>x.layout.rows.length)请尝试以下操作:

var选项卡=[
{
名称:“主选项卡”,
布局:{
标签:“侧面”,
行:[
[{col:12,字段:[“name”]},
[{col:12,字段:[“status”]},
[{col:12,字段:[“model”]},
[{col:12,字段:[“office”]},
[{col:12,字段:[“room”]}]
]
}
},
{
名称:“第二个选项卡”,
布局:{
标签:“侧面”,
行:[{col:12,字段:[“type”]}],{col:12,字段:[“ip”]}]]
}
},
{
名称:“最后一个选项卡”,
布局:{
标签:“侧面”,
行:[
[{col:12,字段:[“office”]},
[{col:12,字段:[“位置”]},
[{col:12,字段:[“地址”]}]
]
}
}
];
var stack=[“位置”、“地址”、“房间”、“办公室”];
console.log(tabs.map(x=>(x.layout.rows=x.layout.rows.filter(row=>stack.includes(row[0].字段[0])),x))

.filter(x=>x.layout.rows.length)您提供的javascript实际上是不可维护的。只是一点建议——仅仅因为你可以在一行中完成它并不意味着你应该这样做。你提供的javascript实际上是不可维护的。仅仅是一点建议——仅仅因为你可以在一行中完成它并不意味着你应该这样做。