首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS筛选器在另一个筛选器中不起作用

JS筛选器在另一个筛选器中不起作用
EN

Stack Overflow用户
提问于 2019-06-25 18:11:31
回答 1查看 65关注 0票数 1

我需要过滤后的标签。只有那些字段来自"stack“列表的字段。因此,我需要首先过滤“field”数组,如果没有,则排除col、row或tab。

代码语言:javascript
复制
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"]

但它不起作用。一点都不懂。

尝试这样做:

代码语言:javascript
复制
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)

预期结果如下:

代码语言:javascript
复制
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"] } ]
      ]
    }
  }
]

但是我得到了相同的tabs数组

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-25 18:41:12

试试这个:

代码语言:javascript
复制
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"];

console.log(tabs.map(x => (x.layout.rows = x.layout.rows.filter(row => stack.includes(row[0].fields[0]) ), x))
    .filter(x => x.layout.rows.length));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56751643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档