我需要过滤后的标签。只有那些字段来自"stack“列表的字段。因此,我需要首先过滤“field”数组,如果没有,则排除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"] } ]
]
}
}
]但是我得到了相同的tabs数组
发布于 2019-06-25 18:41:12
试试这个:
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));
https://stackoverflow.com/questions/56751643
复制相似问题