我有这样的JSON数据:
<script>
var data_work_in =[
{"ID":13,"share":36,"CQty":0,"CPrice": 1},
{"ID":14,"share":36,"CQty":0,"CPrice": 1}
]
data_work_in.CQty = data_work_in.ClipQty%
</script>
我想将CQty的JSON标头重命名为ClipQty%。
请帮助
答案 0 :(得分:1)
如果列未链接,则需要迭代所有元素 您可以创建新列,分配值,然后删除现有的列
var data_work_in =[
{"ID":13,"share":36,"CQty":0,"CPrice": 1},
{"ID":14,"share":36,"CQty":0,"CPrice": 1}
];
data_work_in.forEach(e=>{
e["ClipQty%"]=e['CQty'];delete e["CQty"];
})
console.log(data_work_in)
答案 1 :(得分:1)
使用映射来创建具有所需键和值的新数组,而不是对原始数组进行变异
let orgArray = [{
"ID": 13,
"share": 36,
"CQty": 0,
"CPrice": 1
},
{
"ID": 14,
"share": 36,
"CQty": 0,
"CPrice": 1
}
]
let newArray = orgArray.map(function(item) {
return Object.assign({}, {
"ID": item.ID,
"share": item.ID,
"ClipQty%": item.ID,
"CPrice": item.ID
})
})
console.log(newArray)
答案 2 :(得分:1)
您无法替换,您将不得不添加一个新密钥,为其分配值,然后删除原始密钥。
这应该适用于大多数(非IE)浏览器:
var data_work_in =[
{"ID":13,"share":36,"CQty":0,"CPrice": 1},
{"ID":14,"share":36,"CQty":0,"CPrice": 1}
];
Array.prototype.forEach.call(data_work_in, function(it){
if(it.hasOwnProperty("CQty")){
it["ClipQty"] = it.CQty;
delete it["CQty"]
}
});