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

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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