1

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

4
  • 1
    Split it out into a call to filter with the condition chained with a map Commented Dec 22, 2021 at 11:40
  • 1
    Usually you'll perform a .filter() first then perform your .map() operation. Otherwise if you want to do it in the one pass, you can use .flatMap() and map to an empty array when you want to pass (not really recommended), or use .reduce() (or use a plain for loop, but that will look strange as a one-liner) Commented Dec 22, 2021 at 11:40
  • Thank guys, What about doing .filter(Boolean) after mapping? I would like to achieve as best performance as possible, would it be better to filter it before or after mapping? Commented Dec 22, 2021 at 11:42
  • 1
    @JustPawel mapping will always give you an array of the same length as the array you were originally mapping, so if your data has a length of N, then mapping will produce an array of size N, so if you put the filter after the map, the filter would need to do N iterations. If you do the filter first, that will need to do N iterations on your data originally but will produce a potentially smaller array size of length M, so the map would need to do M iterations in this case, which may be smaller than N (the length of data). So putting the filter first would be more optimal Commented Dec 22, 2021 at 11:45

2 Answers 2

2

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]);
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

The .map() method shouldn't be used if you're not using the array it returns. Instead, .forEach() is a better option, as that's designed for doing just iteration.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.