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

246
Visualizações
How to use one-liner if without else statement in .map() in Javascript?

I have an issue which I do not how to resolve.

I want to map data with .map((x) => x.data) with one-liner if statement inside, but without any else statement. I would like to achieve more or else 'pass' just as in Python.

In example:

What I want:

let mappedData = data.map((x) => x.data === filters.data ? [x.value, x.name, x.category]: pass);

This should return mappedData with data that only fill the condition.

When I use sth like this

let mappedData = data.map((x) => x.data === filters.data ? [x.value, x.name, x.category]: {});

It returns array of size data and with empty dicts if condition is not filled.

I tried as well sth like this:

let mappedData = data.map((x) => x.data === filters.data && [x.value, x.name, x.category]);

But that gives me false at each index that does not fill the condition.

I really would like to do one-liner if possible.

Thanks guys

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can't omit elements with a map call, but you can use filter to do that, and then map the remaining elements:

let mappedData =
   data.filter((x) => x.data === filters.data)
       .map((x) => [x.value, x.name, x.category]);
about 4 years ago · Santiago Trujillo Relatório

0

Not exactly sure what you want, but will this help?

let data = [{name: "Peter", age:29}, {name: "Rina", age: 36}, {name: "Joseph", age: 37}];
let filters = {name: "Rina", age: 36}; 
let res=[];
data.map((item) => {(item.name === filters.name)? res.push(item):null});
console.log(res);
let mappedData = data.map((x) => x.data === filters.data ? [x.value, x.name, x.category]: pass);

about 4 years ago · Santiago Trujillo 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