Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda