I have one complex object and want to extract some keys and apply some function and assign it with some other variable name and using object destructuring syntax but couldn't find any solution to apply.
const alpha = { a: 'lower', b: '23.45' };
const { a: newA.toUpperCase(), b: parseFloat(floatB)} = alpha;
I know this is wrong because here newA
and floatB
is yet not defined.
Even I tried
const { a:a.toUpperCase(), b: parseFloat(b)} = alpha;
But that also not work
So my question is how can we achieve somehow.
Or we need to do it later once assign as new variable name?
You can't do that at the same time. In spite of the destructuring you're declaring / creating variables. While you're creating variables you can't execute a function.
So, STEP 1 -> Destructure what you need from alpha. STEP 2 -> execute the functions you need
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