angularjs - 我想重命名从 api 调用中获取的 json key

标签 angularjs json database key rename

我的数据库中有这种格式的 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/

相关文章:

javascript - AngularJs 指令问题

javascript - 手动引导/初始化时访问 Angular 服务

javascript - AngularJs 计数器可计数到特定目标数

json - 如何将 Django 查询集输出为 JSON?

Python通用数据库接口(interface)?

PHP MYSQL $行[$变量]

SQL 表 : Can I Pivot/Join with N rows?

angularjs - 根据过滤后的数据更新分页

javascript - 查找使用 vue-cli 生成的 Webpack-simple 和 Vuejs 图像的问题

javascript - 如何将 JSON 字符串日期转换为自定义格式日期?