var myNewKeyNames = ['asas','asas'];
var obj =[{key:'value',key:'value'}]
for(var i =0; i<obj.length; i++) {
for(key in the obj[i]) {
alert(key) // gives the keys in the object
alert(myNewKeyNames) instead of key in the object which would rep the values
alert(value) // gives the value in the object
}
}
现在我可以将键转换为其他值......而不使用循环的键值。我尝试更换...像这样...但它不采取...
我不想在初始阶段重命名 key ,只是当 key 打印在textboxxx中时我想将其命名为不同的...
最佳答案
据我了解:
var myNewKeyNames = ['new_key1','new_key2'];
var obj =[ { key1: 'value1', key2: 'value2' } ];
for(var i = 0; i < obj.length; i++) {
var j = 0;
for(key in obj[i]) {
alert(myNewKeyNames[j++] + ': ' + obj[i][key]);
}
}
关于javascript - 在运行时用重复值替换对象的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6958378/