2013-08-02 43 views
0

我有這樣的一個JSON結構:對象數組 - 快速轉換

enter image description here

這是從後端(我用火力地堡)生成,並把一個變量:

var newJson = {} 

我正在使用AngularJS和ng-repeat通過這個變量來顯示結果 - 它的工作原理。我倒要通過訂購票財產JSON和我試圖使用angularJS

| orderBy: 'vote' " 

但是,這並不工作,因爲我是「NG-重複」雖然JSON,而不是陣列。 我試過不同的解決方案,在https://github.com/angular/angular.js/issues/1286 顯示,但我不能讓他們任何工作。

所以我認爲要走容易的道路,並將我的JSON轉換爲數組。 我已經試過:

var arreyM = []; 
for (var i = 0; i < $scope.newJson.length; i++) { 
     arreyM.push(name: $scope.newJson[i].name, pic: $scope.newJson[i].pic, vote:$scope.newJson[i].vote); 
} 

,但它給了我錯誤 - 我猜的語法是錯誤的。

回答

4

試試這個

var arreyM = []; 
for (var i = 0; i < $scope.newJson.length; i++) { 
     arreyM[i]['name']= $scope.newJson[i].name; 
     arreyM[i]['pic']= $scope.newJson[i].pic 
     arreyM[i]['pic'] = $scope.newJson[i].vote; 
} 

希望這將有助於

+0

IT方面康壽日誌中:[] ......我不需要在阿雷的數據以某種方式「推」? –

+1

你必須創建對象'arreyM [i] = {};'並且'$ scope.newJson.length'將會是不確定的,因爲newJson不是一個數組... – Yann

+1

@AlexGarulli嘗試一下,我們把數據像arreyM [I] [ '名稱'] –