2016-01-25 87 views
0

我想清理JSON數據,即刪除了N/A和空數據。我試圖使用$ .each來清理數據,但它對數據沒有影響。清理JSON數據

.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 
    }); 

回答

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 
    }); 
}); 
+0

我有jQuery包括,我使用Ionic和AngularJS,我試過這個解決方案,但它不工作,恐怕它仍然沒有影響數據。 – Snow