【发布时间】:2020-10-13 10:05:47
【问题描述】:
通过重命名对象的键,我失去了价值。如何确保仅更改键而保留值?
这是我的目标
const items = {
"brand": "losee",
"target": "brand",
"item": {
"name": null,
"fabric": null,
"country": "CH",
"fit": "regular"
}
}
// for renaming the key I use this method
delete Object.assign(items, { prototype: items.item.fit }).fit;
console.log(items)
在这个方法之后,我的对象看起来像这样
{
"brand": "losee",
"target": "brand",
"item": {
"name": null,
"fabric": null,
"country": "CH",
"prototype": null
}
}
我做错了什么? 谢谢大家
【问题讨论】:
-
问题中的输出是错误的。
Object.assign向主items对象添加了一个prototype键。
标签: javascript object key key-value