简体   繁体   English

在 javascript 上使用平面图,有条件

[英]use flatmap on javascript, with condition

I am creating a component which handles watch and edit permissions.我正在创建一个处理监视和编辑权限的组件。 The data structure I use to hold the permissions looks like this:我用来保存权限的数据结构如下所示:

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
  }
]

At first, I just wanted to get a list of all users with any permissions given at all, so I would use起初,我只是想获取所有具有任何权限的用户的列表,所以我会使用

let managers = _.flatMap(props.settings.managerPermissions, (p) => p.userId);
managers = [...new Set(managers)];

So this would leave me with managers = [14,24,34,44] , but I need to edit this flatmap in a way that I would not get back the id of a manager which has no permissions at all, even if they were added to the list already, so in this case the return value of the new flatmap should be managers = [14,24] .所以这会给我留下managers = [14,24,34,44] ,但我需要编辑这个平面图,这样我就不会找回根本没有权限的经理的 ID,即使他们是已经添加到列表中,所以在这种情况下,新平面图的返回值应该是managers = [14,24] (ofcourse flatMap is not a must here and if there's a better way to do it I would be happy to see it) (当然 flatMap 在这里不是必须的,如果有更好的方法,我会很高兴看到它)

You only need flatMap when you when the result of map would an array of arrays, and you want a flat array.当 map 的结果是 arrays 的数组时,您只需要 flatMap,并且您需要一个平面数组。

In this case filter the array by isAllowed and then map to userId :在这种情况下,通过isAllowed过滤数组,然后将 map 过滤到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)

However, you actually use Array.flatMap() (or lodash equivalent) to map and filter at the same time.但是,您实际上对 map 使用Array.flatMap() (或 lodash 等效项)并同时进行过滤。 It's less idiomatic the map/filter combo, and I'm not sure about the performance, so I don't use it myself.地图/过滤器组合不太惯用,我不确定性能,所以我自己不使用它。 The idea is to return an empty array for items that you don't want:这个想法是为您不想要的项目返回一个空数组:

 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.flatMap(o => o.isAllowed? o.userId: []) )] console.log(managers)

You should be able to just filter out the isAllowed=false and then do the same - although flatMap seems to be useless (as there is no nesting of arrays) - just use map您应该能够过滤掉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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM