Can the props in a destructuring assignment be transformed in place?

tjfwalker

This works…

const { prop1:val1, prop2:val2 ) = req.query
val1 = val1.toLowerCase()

Though, I'm more inclined to do something like

const { prop1.toLowerCase():val1, prop2:val2 } = req.query

or

const { prop1:val1.toLowerCase(), prop2:val2 } = req.query

neither of which work. Is there a syntax similar to this or must manipulations be done outside of the destructing assignment?

Bergi

No, this is not possible. A destructuring assignment does only assign, it does not do arbitrary transformations on the value. (Setters are an exception, but they would only complicate this).

I would recommend to write

const { prop1, prop2:val2 ) = req.query;
const val1 = prop1.toLowerCase();

or, in one statement:

const { prop1, prop2:val2 ) = req.query, val1 = prop1.toLowerCase();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I implement destructuring assignment for functional react components props?

Must use destructuring props assignment

Must use destructuring props assignment (react/destructuring-assignment)

Must use destructuring props assignment error in constructor

Must use destructuring props assignment in className

Reactjs ; destructuring props assignment with ESLint rules

import props, destructuring assignment & unresolved variable warning

Must use destructuring props assignment issue

Is there a way I can restructure part of a destructuring assignment?

How i can destructuring {this.props.children}?

How can I use this in the left side of the object destructuring assignment?

Can I use destructuring assignment with immutable.js?

Can I pre-declare variables for destructuring assignment of objects?

After destructuring assignment with 'let', can't I ressign a variable of them?

How to namespace Destructuring assignment?

Is this an example of destructuring assignment?

Destructuring assignment, but without a pattern

Python assignment destructuring

Object property assignment with destructuring?

Destructuring assignment for nested properties

Destructuring assignment and variable swapping

Destructuring assignment in object creation

Destructuring assignment and null coalescing

typescript destructuring assignment with interface

Destructuring assignment in Typescript

JavaScript Destructuring Assignment

JS Proxy & destructuring assignment

Destructuring props in ReactJS

component props destructuring error