清理JSON数据

时间:2016-01-25 11:08:19

标签: angularjs json

我正在尝试清理我拥有的JSON数据,即删除N / A和null数据。我正在尝试使用$ .each来清理数据,但它对数据没有影响。

.controller('APICtrl', function($scope, $http, $localstorage, $window, $state, $sce) {
// Search function
//var offices = [];
var n;
  $scope.query = {}
  $scope.queryBy = '$'
// gets the data from offices.json
 $http.get('js/offices.json').then(function(resp) {
    console.log('Success', resp);
    $scope.offices = $.each(resp.data.office, function(key,value){
   //console.log(value);
    if(value==""||value==null){
        delete resp.data.office[key];
    }
});
    //console.log(offices)
  }, function(err) {
    console.error('ERR', err);
    // err.status will contain the status code
  });

2 个答案:

答案 0 :(得分:0)

instean jquery函数

$scope.offices = $.each(resp.data.office, function(key,value){

使用标准角度函数

$scope.offices = angular.forEach(resp.data.office, function(key,value){

https://docs.angularjs.org/api/ng/function/angular.forEach

答案 1 :(得分:0)

你有jQuery吗?否则,我的猜测是你因为它在then内而得到了沉默的错误。这应该有效:

.controller('APICtrl', function($scope, $http, $localstorage, $window, $state, $sce) {
  // Search function
  //var offices = [];
  var n;
  $scope.query = {};
  $scope.queryBy = '$';
      // gets the data from offices.json
  $http.get('js/offices.json').then(function(resp) {
      console.log('Success', resp);

      var office = resp.data.office;

      angular.forEach(office, function(value, key){
        if (value == "" || value == null) {
            delete office[key];
        }
      });

      $scope.offices = office;
      //console.log(offices)
  }, function(err) {
      console.error('ERR', err);
      // err.status will contain the status code
  });
});