最佳答案
假设我有以下目标:
const user = {
id: 42,
displayName: "jdoe",
fullName: {
firstName: "John",
lastName: "Doe"
}
};
我只要 id
和 fullName
。
我会做以下事情:
const { id, fullName } = user
小菜一碟,对吧?
现在假设我想根据另一个名为 fields
的变量的值进行解构。
const fields = [ 'id', 'fullName' ]
现在我的问题是: 如何基于键数组进行解构?
我厚颜无耻地尝试了以下方法,但没有成功:
let {[{...fields}]} = user
和 let {[...fields]} = user
。有什么办法可以做到这一点?
谢谢你