从javascript对象复制数据并为其分配新密钥

时间:2017-07-12 05:56:19

标签: javascript jquery object javascript-objects

我正在获取这样的数据

    4706:"APN"
    4743:"Owner Name"
    4754:"Situs Address"
    6231 :"Mailing Address"

在javascript对象中。当我将它复制到新对象时,它会提供相同的输出,而我想用我的键替换它

    0:"APN"
    1:"Owner Name"
    2:"Situs Address"
    3 :"Mailing Address"

有可能吗?我在tblheader中复制这个对象

        tblHeader=features[i].attributes.fields.values;

2 个答案:

答案 0 :(得分:1)

尝试使用Object.values()方法,创建值数组。并使用Array#forEach()迭代数组并附加新对象



var arr = {
  4706: "APN",
  4743: "Owner Name",
  4754: "Situs Address",
  6231: "Mailing Address",
}
var res ={};
Object.values(arr).forEach(function(a,b){
     res[b]=a
})

console.log(res)




答案 1 :(得分:1)

Try this

oldObject = 
{ 
4706:"APN",
4743:"Owner Name",
4754:"Situs Address",
6231 :"Mailing Address" 
};

 newObject = {}
     Object.keys(oldObject).map(function(key, index) {
     newObject[index] = oldObject[key];
  });
console.log(newObject)