2017-10-08 31 views
0

我有一個JSON JS對象。我需要用ng-repeat來複制這些數據。問題是數組的基本密鑰正在改變。我會解決這個問題。這個JSON將嵌套NG重複顯示帶有隨機密鑰的JSON AngularJS

var JSON = { 
     "DK-App": [{ 
      "_id": 1056, 
      "thread_id": 328, 
      "address": "DK-App", 
      "date": 1507470152962, 
      "date_sent": 1507470093000, 
      "protocol": 0, 
      "read": 0, 
      "status": -1, 
      "type": 1, 
      "reply_path_present": 0, 
      "locked": 0, 
      "sub_id": 2, 
      "phone_id": 1, 
      "error_code": 0, 
      "creator": "com.android.mms", 
      "seen": 0, 
      "priority": -1, 
      "oppo_drafts": 1, 
      "sim_id": -1, 
      "oppo_mass": 0, 
      "oppo_timer": 0, 
      "oppo_groupaddress": "DK-App", 
      "oppo_collected": 0, 
      "oppo_sub_date": 1507470093000, 
      "oppo_service_message_sms_type": 0, 
      "deleted": 0, 
      "sync_state": 0, 
      "oppo_sms_type": 0, 
      "$$hashKey": "object:4" 
     }, { 
      "_id": 1028, 
      "thread_id": 328, 
      "address": "DK-App", 
      "date": 1507360655265, 
      "date_sent": 1507360594000, 
      "protocol": 0, 
      "read": 0, 
      "status": -1, 
      "type": 1, 
      "reply_path_present": 0, 
      "locked": 0, 
      "sub_id": 2, 
      "phone_id": 1, 
      "error_code": 0, 
      "creator": "com.android.mms", 
      "seen": 1, 
      "priority": -1, 
      "oppo_drafts": 1, 
      "sim_id": -1, 
      "oppo_mass": 0, 
      "oppo_timer": 0, 
      "oppo_groupaddress": "DK-App", 
      "oppo_collected": 0, 
      "oppo_sub_date": 1507360594000, 
      "oppo_service_message_sms_type": 0, 
      "deleted": 0, 
      "sync_state": 0, 
      "oppo_sms_type": 0, 
      "$$hashKey": "object:27" 
     }, { 
      "_id": 985, 
      "thread_id": 328, 
      "address": "DK-App", 
      "date": 1507198047122, 
      "date_sent": 1507197980000, 
      "protocol": 0, 
      "read": 0, 
      "status": -1, 
      "type": 1, 
      "reply_path_present": 0, 
      "locked": 0, 
      "sub_id": 2, 
      "phone_id": 1, 
      "error_code": 0, 
      "creator": "com.android.mms", 
      "seen": 1, 
      "priority": -1, 
      "oppo_drafts": 1, 
      "sim_id": -1, 
      "oppo_mass": 0, 
      "oppo_timer": 0, 
      "oppo_groupaddress": "DK-App", 
      "oppo_collected": 0, 
      "oppo_sub_date": 1507197980000, 
      "oppo_service_message_sms_type": 0, 
      "deleted": 0, 
      "sync_state": 0, 
      "oppo_sms_type": 0, 
      "$$hashKey": "object:45" 
     }, { 
      "_id": 977, 
      "thread_id": 328, 
      "address": "DK-App", 
      "date": 1507179236636, 
      "date_sent": 0, 
      "read": 0, 
      "status": -1, 
      "type": 1, 
      "locked": 0, 
      "sub_id": 2, 
      "phone_id": 1, 
      "error_code": 0, 
      "creator": "com.android.mms", 
      "seen": 1, 
      "priority": -1, 
      "oppo_drafts": 1, 
      "sim_id": -1, 
      "oppo_mass": 0, 
      "oppo_timer": 0, 
      "oppo_collected": 0, 
      "oppo_service_message_sms_type": 0, 
      "deleted": 0, 
      "sync_state": 0, 
      "oppo_sms_type": 0, 
      "$$hashKey": "object:48" 
     }], 
     "BZ-FPANDA": [{ 
      "_id": 1055, 
      "thread_id": 96, 
      "address": "BZ-FPANDA", 
      "date": 1507469661223, 
      "date_sent": 1507469657000, 
      "protocol": 0, 
      "read": 0, 
      "status": -1, 
      "type": 1, 
      "reply_path_present": 0, 
      "locked": 0, 
      "sub_id": 1, 
      "phone_id": 0, 
      "error_code": 0, 
      "creator": "com.android.mms", 
      "seen": 0, 
      "priority": -1, 
      "oppo_drafts": 1, 
      "sim_id": -1, 
      "oppo_mass": 0, 
      "oppo_timer": 0, 
      "oppo_groupaddress": "BZ-FPANDA", 
      "oppo_collected": 0, 
      "oppo_sub_date": 1507469657000, 
      "oppo_service_message_sms_type": 0, 
      "deleted": 0, 
      "sync_state": 0, 
      "oppo_sms_type": 0, 
      "$$hashKey": "object:5" 
     }, { 
      "_id": 930, 
      "thread_id": 96, 
      "address": "BZ-FPANDA", 
      "date": 1506925028930, 
      "date_sent": 1506925025000, 
      "protocol": 0, 
      "read": 0, 
      "status": -1, 
      "type": 1, 
      "reply_path_present": 0, 
      "locked": 0, 
      "sub_id": 1, 
      "phone_id": 0, 
      "error_code": 0, 
      "creator": "com.android.mms", 
      "seen": 1, 
      "priority": -1, 
      "oppo_drafts": 1, 
      "sim_id": -1, 
      "oppo_mass": 0, 
      "oppo_timer": 0, 
      "oppo_groupaddress": "BZ-FPANDA", 
      "oppo_collected": 0, 
      "oppo_sub_date": 1506925025000, 
      "oppo_service_message_sms_type": 0, 
      "deleted": 0, 
      "sync_state": 0, 
      "oppo_sms_type": 0, 
      "$$hashKey": "object:56" 
     }] 
    } 

這個JSON需要爲每個陣列創建列表。然而每個陣列的鍵是不同的。納克重複不與

ng-repeat="j in JSON" 

在此之前的工作我試圖通過推動基於關鍵只

var result = {}; 
        data.forEach(function(item) { 
         if (!result[item.address]) 
         result[item.address] = [item]; 
         else 
         result[item.address].push(item); 
        }); 

現在我得到一個JSON與「鑰匙」公共N正常化這個數組列表包含陣列。原始的JSON Lools這樣的

我試圖推動普通對象基於關鍵address。 我需要在JSON變量中訪問「DK-App」和「BZ-FPANDA」,並且需要在「DK-APP」和「BZ-FPANDA」中迭代數組。請注意,這些「DK-APP」和「BZ-FPANDA」都是從後端編程的,並且有這樣的巨大列表。問題的陳述是我如何獲得使用JS的數組,以便我可以迭代它

+0

我需要訪問 「DK-應用程序」 和 「BZ-FPANDA」 在JSON變量,需要遍歷這兩個 「DK-APP」 和 「BZ-FPANDA」 裏面的數組。請注意,這些「DK-APP」和「BZ-FPANDA」都是從後端編程的,並且有這樣的巨大列表。問題聲明是如何獲得使用JS的數組,以便我可以遍歷它 – Siddharth

回答

-1

根據您的期望是什麼,您可以展平數據或使用嵌套的ng-repeat其中外部重複迭代主要對象和內一個迭代內陣列

使用嵌套重複:

<div ng-repeat="(k, arr) in JSON"> 
    Key is: {{k}} 
    <div ng-repeat="item in arr"> 
     Item address is : {{item.address}} 
    </div> 
</div> 
+0

我必須通過基於鍵(地址)創建數組來標準化JSOn。這將創建一個基於密鑰地址的新JSON,並將包含所有具有匹配地址密鑰字段值的Array數組 – Siddharth

+0

提供您的預期結果 – charlietfl

0

隨着歸一化的數據

<div ng-repeat="item in $ctrl.data"> 
    Creator of {{ item._id }} is {{ item.creator }} 
</div> 

沒有非標準化數據

<div ng-repeat="data in $ctrl.JSON"> 
    <div ng-repeat="item in data"> 
     Creator of {{ item._id }} is {{ item.creator }} 
    </div> 
</div> 

如果問題仍然存在,請創建一個plunker示例。

0

由於您的數據嵌套在一個級別,因此您可以在模板中編寫嵌套循環以提取對象內的每個項目。

var JSONdata = { 
 
    "DK-App": [{ 
 
    "_id": 1056, 
 
    "thread_id": 328, 
 
    "address": "DK-App", 
 
    "date": 1507470152962, 
 
    "date_sent": 1507470093000, 
 
    "protocol": 0, 
 
    "read": 0, 
 
    "status": -1, 
 
    "type": 1, 
 
    "reply_path_present": 0, 
 
    "locked": 0, 
 
    "sub_id": 2, 
 
    "phone_id": 1, 
 
    "error_code": 0, 
 
    "creator": "com.android.mms", 
 
    "seen": 0, 
 
    "priority": -1, 
 
    "oppo_drafts": 1, 
 
    "sim_id": -1, 
 
    "oppo_mass": 0, 
 
    "oppo_timer": 0, 
 
    "oppo_groupaddress": "DK-App", 
 
    "oppo_collected": 0, 
 
    "oppo_sub_date": 1507470093000, 
 
    "oppo_service_message_sms_type": 0, 
 
    "deleted": 0, 
 
    "sync_state": 0, 
 
    "oppo_sms_type": 0, 
 
    "$$hashKey": "object:4" 
 
    }, { 
 
    "_id": 1028, 
 
    "thread_id": 328, 
 
    "address": "DK-App", 
 
    "date": 1507360655265, 
 
    "date_sent": 1507360594000, 
 
    "protocol": 0, 
 
    "read": 0, 
 
    "status": -1, 
 
    "type": 1, 
 
    "reply_path_present": 0, 
 
    "locked": 0, 
 
    "sub_id": 2, 
 
    "phone_id": 1, 
 
    "error_code": 0, 
 
    "creator": "com.android.mms", 
 
    "seen": 1, 
 
    "priority": -1, 
 
    "oppo_drafts": 1, 
 
    "sim_id": -1, 
 
    "oppo_mass": 0, 
 
    "oppo_timer": 0, 
 
    "oppo_groupaddress": "DK-App", 
 
    "oppo_collected": 0, 
 
    "oppo_sub_date": 1507360594000, 
 
    "oppo_service_message_sms_type": 0, 
 
    "deleted": 0, 
 
    "sync_state": 0, 
 
    "oppo_sms_type": 0, 
 
    "$$hashKey": "object:27" 
 
    }, { 
 
    "_id": 985, 
 
    "thread_id": 328, 
 
    "address": "DK-App", 
 
    "date": 1507198047122, 
 
    "date_sent": 1507197980000, 
 
    "protocol": 0, 
 
    "read": 0, 
 
    "status": -1, 
 
    "type": 1, 
 
    "reply_path_present": 0, 
 
    "locked": 0, 
 
    "sub_id": 2, 
 
    "phone_id": 1, 
 
    "error_code": 0, 
 
    "creator": "com.android.mms", 
 
    "seen": 1, 
 
    "priority": -1, 
 
    "oppo_drafts": 1, 
 
    "sim_id": -1, 
 
    "oppo_mass": 0, 
 
    "oppo_timer": 0, 
 
    "oppo_groupaddress": "DK-App", 
 
    "oppo_collected": 0, 
 
    "oppo_sub_date": 1507197980000, 
 
    "oppo_service_message_sms_type": 0, 
 
    "deleted": 0, 
 
    "sync_state": 0, 
 
    "oppo_sms_type": 0, 
 
    "$$hashKey": "object:45" 
 
    }, { 
 
    "_id": 977, 
 
    "thread_id": 328, 
 
    "address": "DK-App", 
 
    "date": 1507179236636, 
 
    "date_sent": 0, 
 
    "read": 0, 
 
    "status": -1, 
 
    "type": 1, 
 
    "locked": 0, 
 
    "sub_id": 2, 
 
    "phone_id": 1, 
 
    "error_code": 0, 
 
    "creator": "com.android.mms", 
 
    "seen": 1, 
 
    "priority": -1, 
 
    "oppo_drafts": 1, 
 
    "sim_id": -1, 
 
    "oppo_mass": 0, 
 
    "oppo_timer": 0, 
 
    "oppo_collected": 0, 
 
    "oppo_service_message_sms_type": 0, 
 
    "deleted": 0, 
 
    "sync_state": 0, 
 
    "oppo_sms_type": 0, 
 
    "$$hashKey": "object:48" 
 
    }], 
 
    "BZ-FPANDA": [{ 
 
    "_id": 1055, 
 
    "thread_id": 96, 
 
    "address": "BZ-FPANDA", 
 
    "date": 1507469661223, 
 
    "date_sent": 1507469657000, 
 
    "protocol": 0, 
 
    "read": 0, 
 
    "status": -1, 
 
    "type": 1, 
 
    "reply_path_present": 0, 
 
    "locked": 0, 
 
    "sub_id": 1, 
 
    "phone_id": 0, 
 
    "error_code": 0, 
 
    "creator": "com.android.mms", 
 
    "seen": 0, 
 
    "priority": -1, 
 
    "oppo_drafts": 1, 
 
    "sim_id": -1, 
 
    "oppo_mass": 0, 
 
    "oppo_timer": 0, 
 
    "oppo_groupaddress": "BZ-FPANDA", 
 
    "oppo_collected": 0, 
 
    "oppo_sub_date": 1507469657000, 
 
    "oppo_service_message_sms_type": 0, 
 
    "deleted": 0, 
 
    "sync_state": 0, 
 
    "oppo_sms_type": 0, 
 
    "$$hashKey": "object:5" 
 
    }, { 
 
    "_id": 930, 
 
    "thread_id": 96, 
 
    "address": "BZ-FPANDA", 
 
    "date": 1506925028930, 
 
    "date_sent": 1506925025000, 
 
    "protocol": 0, 
 
    "read": 0, 
 
    "status": -1, 
 
    "type": 1, 
 
    "reply_path_present": 0, 
 
    "locked": 0, 
 
    "sub_id": 1, 
 
    "phone_id": 0, 
 
    "error_code": 0, 
 
    "creator": "com.android.mms", 
 
    "seen": 1, 
 
    "priority": -1, 
 
    "oppo_drafts": 1, 
 
    "sim_id": -1, 
 
    "oppo_mass": 0, 
 
    "oppo_timer": 0, 
 
    "oppo_groupaddress": "BZ-FPANDA", 
 
    "oppo_collected": 0, 
 
    "oppo_sub_date": 1506925025000, 
 
    "oppo_service_message_sms_type": 0, 
 
    "deleted": 0, 
 
    "sync_state": 0, 
 
    "oppo_sms_type": 0, 
 
    "$$hashKey": "object:56" 
 
    }] 
 
} 
 

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

 
module.controller("MainCtrl", function($scope) { 
 
    $scope.messages = JSONdata; 
 
});
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
</head> 
 

 
<body ng-app="myApp"> 
 
    <div ng-controller="MainCtrl"> 
 

 
    <div ng-repeat="(address, messageList) in messages"> 
 
     <h3>{{address}}</h3> 
 
     <ul> 
 
     <li ng-repeat="message in messageList"> 
 
      <pre>{{message | json}}</pre> 
 
     </li> 
 
     </ul> 
 
    </div> 
 
    </div> 
 

 
</body> 
 

 
</html>