Is it possible inside ?:
change dictionary and return this dictionary in the same block. Something like this a > b ? <dict['c'] = 'I'm changed', return dict> : <some other code>;
I need this inside map
function. Something like this:
var a = [...]
a.map((itm) => return itm.isComposite ? itm[key] = transform(itm[data]); return itm : transform(itm))
Yes, it's possible by using the comma operator:
a > b ? (dict['c'] = "I'm changed", dict) : <some other code>;
The comma operator evaluates all of its operands and returns the value of the last (rightmost) one.
However, a proper if
statement might be the more readable solution here, which is why I'd recommend you use it instead of constructing a conditional ("ternary") expression.
The comma operator should be enough. No need to use the return
keyword.
var result = (a > b
? (dict['c'] = "I'm changed", dict)
: <some other code> );
If you want to return the result then it goes outside the expression:
return (a > b
? (dict['c'] = "I'm changed", dict)
: <some other code> );
That said, I would prefer using if statements for this problem. In my opinion, long ternary expressions are hard to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With