Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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]. (ofcourse flatMap is not a must here and if there's a better way to do it I would be happy to see it)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You only need flatMap when you when the result of map would an array of arrays, and you want a flat array.

In this case filter the array by isAllowed and then map to 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. 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)

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

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);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda