我正在創建一個處理監視和編輯權限的組件。我用來保存權限的資料結構如下所示:
const input = [{
isAllowed: true,
permissionType: "watch",
userId: 14
},
{
isAllowed: false,
permissionType: "edit",
userId: 14
},
{
isAllowed: true,
permissionType: "edit",
userId: 24
},
{
isAllowed: false,
permissionType: "edit",
userId: 34
},
{
isAllowed: false,
permissionType: "watch",
userId: 44
},
{
isAllowed: false,
permissionType: "edit",
userId: 44
}
]
起初,我只是想獲得具有任何權限的所有用戶的串列,所以我會使用
let managers = _.flatMap(props.settings.managerPermissions, (p) => p.userId);
managers = [...new Set(managers)];
所以這會給我留下managers = [14,24,34,44],但我需要以一種方式編輯這個平面地圖,即使他們已經被添加到串列中,我也不會取回根本沒有權限的經理的 id,所以在這種情況下新平面圖的回傳值應該是managers = [14,24]. (當然 flatMap 在這里不是必須的,如果有更好的方法,我會很高興看到它)
uj5u.com熱心網友回復:
只有當 map 的結果是一個陣列陣列時才需要 flatMap,并且您需要一個平面陣列。
在這種情況下,過濾陣列isAllowed,然后映射到userId:
const arr = [{"isAllowed":true,"permissionType":"watch","userId":14},{"isAllowed":false,"permissionType":"edit","userId":14},{"isAllowed":true,"permissionType":"edit","userId":24},{"isAllowed":false,"permissionType":"edit","userId":34},{"isAllowed":false,"permissionType":"watch","userId":44},{"isAllowed":false,"permissionType":"edit","userId":44}]
const managers = [...new Set(
arr
.filter(o => o.isAllowed)
.map(o => o.userId)
)]
console.log(managers)
uj5u.com熱心網友回復:
您應該能夠過濾掉isAllowed=false然后執行相同的操作-盡管flatMap似乎沒用(因為沒有嵌套陣列)-只需使用map
const input = [
{isAllowed: true,
permissionType: "watch",
userId : 14
},
{isAllowed: false,
permissionType: "edit",
userId : 14
},
{isAllowed: true,
permissionType: "edit",
userId : 24
},
{isAllowed: false,
permissionType: "edit",
userId : 34
},
{isAllowed: false,
permissionType: "watch",
userId : 44
},
{isAllowed: false,
permissionType: "edit",
userId : 44
}]
const managers = [...new Set(input.filter(x => x.isAllowed).map(x => x.userId))];
console.log(managers);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358448.html
標籤:javascript 洛达什 平面图
