【发布时间】:2020-07-04 20:30:28
【问题描述】:
我有两个对象数组。我想根据 PermissionObj 过滤数据。
这是来自数据库。这是 permissionObj 中的子数组数组。
const PermissionObj = {
permission: [{
"books": [{
"label": "Can View",
"value": "can_view"
}]
},
{
"Journals": [{
"label": "Can View",
"value": "can_view"
},
{
"label": "Can Create",
"value": "can_create"
},
]
},
{
"deal": [{
"label": "Can update",
"value": "can_update"
},
{
"label": "Can delete",
"value": "can_delete"
},
]
}
]
}
这是静态数据。我想根据 PermissionObj 比较这个数据。
const data = [{
label: "books",
value: "can_view"
},
{
label: "deal",
content: [{
value: "can_update"
}, {
value: "can_delete"
},{value:"can_view"}]
},
{
label: "Articles",
},
{
label: "Journals",
content: [{
value: "can_create"
},
{
value: "can_view"
},
{
value: "can_delete"
},
{
value: "can_edit"
},
]
}
]
我正在尝试根据 PermissionObj 对象数组 过滤 对象数据数组。这是我尝试的代码。
let permission = PermissionObj.permission
permission.filter(item=>{
// I am finding data label by using permission label.
let dataItem = data.find(index=>item[index.label])
console.log(dataItem)
})
// the problem is that I want to filtering based on value .
// if both of value is true , then show the data .
如果 PermissionObj 值将与数据值匹配。然后显示数据。
我接受的输出将是这种格式:
const data = [{
label: "books",
value: "can_view"
},
{
label: "deal",
content: [{
value: "can_update"
}, {
value: "can_delete"
}]
},
{
label: "Journals",
content: [{
value: "can_create"
},
{
value: "can_view"
},
]
}
]
【问题讨论】:
-
我看到输出未定义
-
filter 返回一个数组,所以你需要将它分配给一个变量
let filtered = permission.filter... -
你看这个。我真的很麻烦这个问题
标签: javascript arrays node.js reactjs object