我需要过滤标签。仅那些字段来自“堆栈”列表。 因此,我需要先过滤“字段”数组,然后如果没有任何内容,则排除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"] } ]
]
}
}
]
但是我得到了相同的标签数组
答案 0 :(得分:0)
尝试一下:
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));