javascript - 在运行时用重复值替换对象的键

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

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

我不想在初始阶段重命名 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]);
    }
}

Check it!

关于javascript - 在运行时用重复值替换对象的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6958378/

相关文章:

javascript - 如何使用 StackNavigator 将数据从一个屏幕内的列表传递到下一个屏幕

javascript - document.getElementById ("").innerHTML= 不起作用(但只是部分起作用)

javascript - 使用 React 处理的 onClick 创建 anchor

JavaScript Row Transposition Cipher - 如何将键转换为列号

javascript - 模态 - 浏览器后退按钮停留在同一站点上

javascript - 将复选框绑定(bind)到 boolean 变量

javascript - 为什么我不能使用Ext.grid.Grid?

javascript - Number 作为默认的 jquery 插件选项

javascript - 表单提交事件中 this.submit() 的行为

php - 如何在 JavaScript 和 PHP 中对 URL 进行编码?