我的数据库中有这种格式的 json。
[
{
ip.src:"192.168.200.10",
y:1506
},
{
ip.src:"192.168.200.10",
y:1506
},
{
ip.src:"192.168.200.10",
y:1506
},
{
ip.src:"192.168.200.10",
y:1506
},
{
ip.src:"192.168.200.10",
y:1506
}
]
我想把键名ip.src
改成name
对于使用循环的所有值
这是我的代码
$http.get("api/data")
.then(function(data,status){
if(data){
$scope.log=data;
angular.forEach($scope.log, function(value, key){
if(key=="data"){
$scope.log.data.ip.src=name;
}
}
}
最佳答案
一个简单的Array.map()这就是您需要的。
const array = [{
"ip.src": "192.168.200.10",
y: 1506
},
{
"ip.src": "192.168.200.10",
y: 1506
},
{
"ip.src": "192.168.200.10",
y: 1506
},
{
"ip.src": "192.168.200.10",
y: 1506
},
{
"ip.src": "192.168.200.10",
y: 1506
}
];
const changed = array.map(item => {
item.name = item['ip.src'];
delete item['ip.src'];
return item;
});
console.log(changed);
关于angularjs - 我想重命名从 api 调用中获取的 json key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47010287/