不要重复已经重复的项目

时间:2016-07-13 11:35:54

标签: javascript angularjs

我有一个问题,我的回复如下:

[
{
    "quesListName": "Angular Data Binding Question List",
    "questions": "data binding between factory and view"
},
{
    "quesListName": "Angular Data Binding Question List",
    "questions": "data binding between factory and view"
},
{
    "quesListName": "Angular Filters Question List",
    "questions": "how to use filter date in directive in angular js ?"
},
{
    "quesListName": "Angular Filters Question List",
    "questions": " directive in angular js ?"
},
{
    "quesListName": "Controller Question List",
    "questions": " filter in angular js ?"
}

所以我需要在这里重复quesListName,但是当我显示它时,questListName不应该重复

<ul>
  <li ng-repeat="qlist in response">
            {{qlist}}
   </li>           
 </ul>

但是这里我得到的所有qList都是已经重复的那个 我需要像

这样的输出
Angular Data Binding Question List
Angular Filters Question List
Controller Question List

任何帮助都非常感谢

2 个答案:

答案 0 :(得分:3)

您可以使用public class User { private boolean admin; private String alias; private String email; @JsonDeserialize(using = StringValueDeserializer.class) private String emailId; @JsonDeserialize(using = StringArrayValueDeserializer.class) private ArrayList<String> groups; @JsonDeserialize(using = StringValueDeserializer.class) private String id; private boolean locked; private int loggedInCount; private boolean master; private String sms; @JsonDeserialize(using = StringValueDeserializer.class) private String smsId; private String type; private String username; .... getter and setter } 来实现此目的。所以你需要添加angular-filter脚本并在模块中添加依赖项。

在index.html中:在angular-filter脚本

之后加载
angular.js

在你的模块中:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.8/angular-filter.min.js"></script>

在HTML中:

angular.module('appName', ['angular.filter']);

答案 1 :(得分:0)

您可以在没有任何的情况下执行此操作,如下所示:

&#13;
&#13;
(function() {
  "use strict";
  angular.module('app', [])
    .controller('mainCtrl', function($scope) {
      $scope.array = [  
         {  
            "quesListName":"Angular Data Binding Question List",
            "questions":"data binding between factory and view"
         },
         {  
            "quesListName":"Angular Data Binding Question List",
            "questions":"data binding between factory and view"
         },
         {  
            "quesListName":"Angular Filters Question List",
            "questions":"how to use filter date in directive in angular js ?"
         },
         {  
            "quesListName":"Angular Filters Question List",
            "questions":" directive in angular js ?"
         },
         {  
            "quesListName":"Controller Question List",
            "questions":" filter in angular js ?"
         }
      ];

      $scope.response = [];
      var unique = {};
      $scope.array.forEach(function(item) {
        if (!unique[item.quesListName]) {
          $scope.response.push(item);
          unique[item.quesListName] = item;
        }
      });
    });
})();
&#13;
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="mainCtrl">
  <span>With duplicates:</span>
  <ul>
    <li ng-repeat="qlist in array" ng-bind="qlist.quesListName">
    </li>
  </ul>
  <span>Without duplicates:</span>
  <ul>
    <li ng-repeat="qlist in response" ng-bind="qlist.quesListName">
    </li>
  </ul>
</body>

</html>
&#13;
&#13;
&#13;

注意:如果您使用 ES6 ,则可以通过以下方式删除重复项:

$scope.response = $scope.array.filter((thing, index, self) => self.findIndex((t) => {
   return t.quesListName == thing.quesListName;
}) === index);