2017-02-04 34 views
0

一個JSON陣列內的兩個項目這是我的代碼:如何分割的角度

$scope.center_name = []; 
     $scope.stats = ["Stats"]; 
     $scope.totMemCenterData = []; 


     var query = "SELECT count(*) as tot_mem, centers.name as center_name FROM mem_groups " 
    + "INNER JOIN centers ON mem_groups.center_id=centers.id GROUP BY centers.id"; 
     $cordovaSQLite.execute(db, query, []) 
     .then(function(res){ 
      if (res.rows.length > 0) { 
      for (var i=0; i < res.rows.length; i++) { 
      $scope.totMemCenterData = res.rows.item(i); 
      console.log(JSON.stringify($scope.totMemCenterData)); 

      } 
     } 
     }, function(err){ 
      // $cordovaToast.showShortBottom('Something Went Wrong').then(function(success){}, function(err){}); 
      console.log(err.message); 
     }); 

這是console.log(JSON.stringify($scope.totMemCenterData));結果:

{"center_name":"AFG - ANONANG","tot_mem":6} 
{"center_name":"BAM - BUENAVISTA","tot_mem":3} 
{"center_name":"GHT - TAGAS","tot_mem":2} 

我希望把所有center_names在一個陣列中還另一個陣列上的tot_mem。我想這是這樣的:

中心: 「AFG - ANONANG」, 「BAM - BUENAVISTA」, 「GHT - TAGAS」 Tot_mem:6,3,2 我要把這些值的圖表上。在x軸和tot_mem在y軸

+0

檢查答案 – Sajeetharan

+0

我會嘗試。謝謝! @Sajeetharan –

+0

標記爲答案,如果它有幫助 – Sajeetharan

回答

0

你可以這樣做中心,

$scope.center_names = []; 
    $scope.tot_mem = []; 
    angular.forEach($scope.sampleTest, function(key, value) { 
    $scope.center_names.push(key["center_name"]); 
    $scope.tot_mem.push(key["tot_mem"]); 
    }); 

DEMO

var app = angular.module('sampleApp', []); 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.sampleTest = [{ 
 
    "center_name": "AFG - ANONANG", 
 
    "tot_mem": 6 
 
    }, { 
 
    "center_name": "BAM - BUENAVISTA", 
 
    "tot_mem": 3 
 
    }, { 
 
    "center_name": "GHT - TAGAS", 
 
    "tot_mem": 2 
 
    }]; 
 
    $scope.center_names = []; 
 
    $scope.tot_mem = []; 
 
    angular.forEach($scope.sampleTest, function(key, value) { 
 
     $scope.center_names.push(key["center_name"]); 
 
     $scope.tot_mem.push(key["tot_mem"]); 
 
    }); 
 
});
<!DOCTYPE html> 
 
<html ng-app="sampleApp" xmlns="http://www.w3.org/1999/xhtml"> 
 
<head> 
 
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.10/angular.min.js"></script> 
 
</head> 
 
<body ng-controller="myCtrl"> 
 
    <h1> Center names </h1> 
 
    <div ng-repeat="item in center_names"> 
 
     {{item}} 
 
     </div> 
 
    <h1> Total memory </h1> 
 
    <div ng-repeat="tot in tot_mem"> 
 
     {{tot}} 
 
     </div> 
 
</body> 
 
</html>

0

一些意見:

  • TypeError: res.rows.item is not a function

    因此,使用這種res.rows.item[i]代替res.rows.item(i).

  • 爲什麼JSON.stringify由於您必須迭代每個對象以基於鍵創建兩個不同的數組。所以,請保留$scope.totMemCenterData

  • 而不是檢查長度res.rows.length檢查項目的長度(res.rows.item.length),因爲您要迭代項目。

工作演示:

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('MyCtrl',function($scope) { 
 
    var res = { 
 
\t "rows": { 
 
\t \t "item": [{ 
 
\t \t \t "center_name": "AFG - ANONANG", 
 
\t \t \t "tot_mem": 6 
 
\t \t }, { 
 
\t \t \t "center_name": "BAM - BUENAVISTA", 
 
\t \t \t "tot_mem": 3 
 
\t \t }, { 
 
\t \t \t "center_name": "GHT - TAGAS", 
 
\t \t \t "tot_mem": 2 
 
\t \t }] 
 
\t } 
 
}; 
 

 
$scope.center_names = []; 
 
$scope.tot_mem = []; 
 
    
 
for (var i=0; i < res.rows.item.length; i++) { 
 
    $scope.totMemCenterData = res.rows.item[i]; 
 
    $scope.center_names.push($scope.totMemCenterData.center_name); 
 
    $scope.tot_mem.push($scope.totMemCenterData.tot_mem); 
 
} 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
<b>Center Names :</b> 
 
<div ng-repeat="names in center_names"> 
 
{{names}} 
 
</div> 
 
<b>Total Mem :</b> 
 
<div ng-repeat="item in tot_mem"> 
 
{{item}} 
 
</div> 
 
</div>

+0

感謝您的更正。 :) –