有没有办法解构
属性名称中有连字符的对象。这样做时我得到了 SyntexError
(这是合理的,因为 JS 不允许变量名带有 hyphen
)。
let human= {
"first-name":"John",
"last-name":"Doe",
age:150
}
let {age}= human;
// let {"first-name"}= human;//error
// let {first-name}= human;//error
console.log(age)
最佳答案
您可以为属性名称起别名 using the colon syntax .
let human = {
"first-name": "John",
"last-name": "Doe",
age: 150
};
let { age, "first-name": firstName } = human;
console.log(age, firstName);
关于javascript - 解构属性名称中包含连字符的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51357215/