Earlier quoted context omitted.
If you are looking for really general and powerful, then there is the mighty reduce: [1, 2, 3, 4, 5].reduce((x, y) => y % 2 === 1 ? [...x, y * 2] : x, [])
The spread operator looks cool and makes just returning the ternary operator work here but its performance implications are non-obvious (it's makin' copies). With reduce() you're really wanting something like this: [1, 2, 3, 4, 5].reduce((x, y) => { if (y % 2 === 1) x.push(y * 2); return x; }, []) I've many times wished that push() would just return the array, it would make reduce() far easier for this sort of use ca…
x.concat([y*2])
would return the array (but makes a duplicate)Anyway, I find this to be a whole lot more sensible:
x=[];
for(y of [1,2,3,4,5]){
if(y%2===1)x.push(y*2)
}
Or even! y=[1,2,3,4,5];
x=[];
// map reduce/flatmap/map/filter etc omg wtf
for( i=0; i
I cant even tell what language this is but there is nothing here that needs fixing.