我有一个像这样的多维对象:
var elements = {
front: {
"1": {
"backgroundImage": "url(images/elements/pig.png)",
},
"2": {
"backgroundImage": "url(images/elements/star.png)",
},
"3": {
"backgroundImage": "url(images/elements/fish.png)",
}
},
top: {},
left: {},
right: {}
};
我需要切换这个对象中的两个元素,所以我这样做:
var temp = elements[front][2];
elements[front][2] = elements[front][3];
elements[front][3] = temp;
这给了我(切换鱼和星 - 但只有属性而不是对象键):
var elements = {
front: {
"1": {
"backgroundImage": "url(images/elements/pig.png)",
},
"2": {
"backgroundImage": "url(images/elements/fish.png)",
},
"3": {
"backgroundImage": "url(images/elements/star.png)",
}
},
top: {},
left: {},
right: {}
};
我怎样才能重命名对象键?
期望的结果:
var elements = {
front: {
"1": {
"backgroundImage": "url(images/elements/pig.png)",
},
"3": {
"backgroundImage": "url(images/elements/fish.png)",
},
"2": {
"backgroundImage": "url(images/elements/star.png)",
}
},
top: {},
left: {},
right: {}
};