在运行时使用重复值替换Object的键

时间:2011-08-05 14:59:28

标签: javascript

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
    }
}

现在我可以将键转换为其他值....而不使用我的循环键值。我试图替换......像这样...但它没有采取......

我不想在初始阶段重命名密钥,只是当密钥在textboxxx中打印时,我想将其命名为不同...

2 个答案:

答案 0 :(得分:1)

不太明白你想要什么......但是试一试。看看下面的链接。

http://jsfiddle.net/qDgTm/

答案 1 :(得分:1)

据我了解:

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]);
    }
}

Check it!

相关问题