对象angularjs的反向对象

时间:2017-07-03 10:41:05

标签: angularjs angularjs-ng-repeat arrayobject

目前我遇到了一个问题,我在数据中有一些问题和关键 我想显示数据是这样的方式,首先我的键应该出现然后我的数据问题在我的数据

    {
  "0": [
    {
      "question": "How often is real property re-assessed (or revalued)?",
      "id": 1,
      "section": "Assessment",
      "sectionId": 2,
      "check": true,
      "index": 0
    },
    {
      "question": "How often second?",
      "id": 3,
      "section": "Assessment",
      "sectionId": 2,
      "check": true,
      "index": 0
    },
    {
      "question": "How often third?",
      "id": 2,
      "section": "Assessment",
      "sectionId": 2,
      "check": true,
      "index": 0
    },
    {
      "key": "Survey Meta Data"
    }
  ],
  "1": [
    {
      "question": "When are New Assessment Notices sent out?",
      "id": 3,
      "section": "Assessment",
      "sectionId": 2,
      "check": true,
      "index": 1
    },
    {
      "key": "Assessment"
    }
  ]
}

当我对这些数据使用ng-repeat时,我得到了这样的结果:

<div ng-repeat="data in viewQuestions">
<div class="panel panel-default"  ng-repeat="value in data">
     {{value.key}}

   label>{{value.question}} </label>

     </div>
      </div>

首先显示我的问题然后显示密钥,但是 我想先显示密钥然后再显示问题 我知道问题首先显示问题,因为它们位于首先插入的数据键中。

是否有任何方法可以撤消数据,因此首先显示然后显示问题

2 个答案:

答案 0 :(得分:0)

您需要的是数据按摩,以将数据结构更改为您需要的格式。

var processedData = [];
angular.forEach(data, function(item) {
  var section = { questions: [] };

  angular.forEach(data, function(question) {
    if (question.key) { // this is key, not question
      section.key = question.key;
    }
    else {
      section.questions.push(question);
    }
  });

  processedData.push(section);
});

答案 1 :(得分:0)

在挖掘之后,我想出了一个完美无缺的解决方案: 首先使用此过滤器反转数组:

 .filter("reverse", function(){
    return function(items){
        return items.slice().reverse(); // Create a copy of the array and reverse the order of the items
    };
});

并在html中添加此行

<div class="panel panel-default"  ng-repeat="value in data |reverse">

现在键首先显示然后显示问题