如何使用 javascript 更改一个对象中的键?
我有:
var myAbc = { 0: true, 1: false, 2: true };
我想更改 de 键,例如:
and i want to change de keys like:
var myAbc = { key1: true, key2: false, key3: true };
我已经试过了:
for (var key in array) {
key = value;
}
但是没有改变数组的键位在for的外面,有什么帮助吗?
but did not change the key of the array out side of the for, any help?
推荐答案
如果你会用es6,你可以一行行搞定:
If you can use es6, you can do this in one line:
var myAbc = { 0: true, 1: false, 2: true };
var renamed = Object.keys(myAbc).reduce((p, c) => { p[`key${Number(c)+1}`] = myAbc[c]; return p; }, {})
console.log(renamed)
相关文章