破坏性任务中的道具可以原地转换吗? [英] Can the props in a destructuring assignment be transformed in place?

查看:82
本文介绍了破坏性任务中的道具可以原地转换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行得通...

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

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?

推荐答案

否,这是不可能的.析构分配仅分配,而不对值进行任意转换. (设置者是一个例外,但只会使情况复杂化.)

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).

我建议写

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

或者,在一项声明中:

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

这篇关于破坏性任务中的道具可以原地转换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆