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
filterwith the condition chained with amap.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).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?datahas 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 yourdataoriginally but will produce a potentially smaller array size of lengthM, 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