Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we apply method along with object destructuring? [duplicate]

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?

like image 277
diEcho Avatar asked Sep 03 '25 06:09

diEcho


1 Answers

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

like image 131
evedes Avatar answered Sep 04 '25 23:09

evedes