Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!