我们假设我有以下对象:
const user = {
id: 42,
displayName: "jdoe",
fullName: {
firstName: "John",
lastName: "Doe"
}
};
Run Code Online (Sandbox Code Playgroud)
而我只想要id和fullName.
我会做以下事情:
const { id, fullName } = user
Run Code Online (Sandbox Code Playgroud)
轻松,对吧?
现在让我们假设我想根据另一个变量的值来进行解构fields.
const fields = [ 'id', 'fullName' ]
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:我如何根据一系列密钥进行解构?
我无耻地尝试了以下但没有成功:
let {[{...fields}]} = user和let {[...fields]} = user.有什么办法可以做到这一点吗?
谢谢