我想要做的是重命名services.XXX
值,但保留他的孩子属性
例如,我想用object.services.cadvisor
object.services.new-name
我该怎么做?
答案 0 :(得分:2)
您可以执行以下操作
object.services.newName = object.services.cadvisor;
delete object.services.cadvisor
答案 1 :(得分:1)
将对象复制到新属性并删除旧属性
let myObj = {
services: {
cadvisor : {
name: 'Cadivsor',
length: 50,
nestedObject : {
name: 'Nested'
}
},
other : {}
}
};
myObj.services['newName'] = myObj.services.cadvisor;
delete myObj.services.cadvisor;
console.log(myObj);