【问题标题】:Remove duplicate values from json array using angular js使用角度js从json数组中删除重复值
【发布时间】:2016-10-13 17:35:33
【问题描述】:

我想从以下 json 数组中删除重复项

$scope.array = [
    {"name":"aaa","key":"1"},
    {"name":"bbb","key":"2"},
    {"name":"aaa","key":"1"},
    {"name":"ccc","key":"3"},
    {"name":"bbb","key":"2"}
];

我尝试了以下代码,但它不起作用

var ids = {};

$scope.array.forEach(function (list) {
    ids[list.name] = (ids[list.name] || 0) + 1;
});

var finalResult = [];
$scope.array.forEach(function (list) {
    if (ids[list.name] === 1) finalResult.push(student);
});

console.log(finalResult);

这是预期的结果。

$scope.array = [
    {"name":"aaa","key":"1"},
    {"name":"bbb","key":"2"} ,
    {"name":"ccc","key":"3"} 
];

【问题讨论】:

标签: javascript arrays angularjs json


【解决方案1】:

您可以使用独特的过滤器。

<tr ng-repeat="arr in array | unique: 'key'" >
   <td> {{ arr.name }} , {{ arr.key }} </td>
</tr>

【讨论】:

    【解决方案2】:

    您可以使用 Array#filter 执行类似的操作

    $scope = {};
    $scope.array = [{
      "name": "aaa",
      "key": "1"
    }, {
      "name": "bbb",
      "key": "2"
    }, {
      "name": "aaa",
      "key": "1"
    }, {
      "name": "ccc",
      "key": "3"
    }, {
      "name": "bbb",
      "key": "2"
    }];
    
    var ids = {};
    
    $scope.array = $scope.array.filter(function(v) {
      var ind = v.name + '_' + v.key;
      if (!ids[ind]) {
        ids[ind] = true;
        return true;
      }
      return false;
    });
    console.log($scope.array);

    【讨论】:

      【解决方案3】:

      你可以使用纯javascript!

      uniqueArray = a.filter(function(item, pos) {
          return a.indexOf(item) == pos;
      })
      

      在你的情况下:

      var finalResult = [];
      finalResult = $scope.array.filter(function(item, pos) {
          return array.indexOf(item) == pos;
      })
      

      【讨论】:

        【解决方案4】:

        你可以使用

        $scope.array = = [{
            "testada": "ecom",
                "id": "27"
        }, {
            "testada": "alorta",
                "id": "27"
        }, {
            "testada": "france24",
                "id": "23"
        }, {
            "testada": "seloger",
                "id": "23"
        }];
        
        var arr = [],
            collection = [];
        
        $scope.array.forEach(json_all, function (index, value) {
            if ($.inArray(value.id, arr) == -1) {
                arr.push(value.id);
                collection.push(value);
            }
        });
        
        console.log(json_all);
        console.log(collection);
        console.log(arr);

        【讨论】:

          【解决方案5】:
           $scope.array = [
                  {"name":"aaa","key":"1"},
                  {"name":"bbb","key":"2"},
                  {"name":"aaa","key":"1"},
                  {"name":"ccc","key":"3"},
                  {"name":"bbb","key":"2"}
              ];
          
              var newArr = [];  //this will be new array with no duplicate value
              for (var i = 0; i < $scope.array.length; i++) {
          
                  if(!ifContains($scope.array[i])){
                      newArr.push($scope.array[i]);
                  }
              }
          
              function ifContains(obj){
                  debugger
                  var flag = false;
                  for(var i = 0; i < newArr.length; i++){
                      if(newArr[i].name == obj.name && newArr[i].key == obj.key )
                          return true;
                      else
                          flag = false;
                  }
                  return flag;
              }
          

          【讨论】:

            【解决方案6】:

            纯JS可以很简单的完成。 Object.prototype.compare() 的发明将极大地帮助我们。让我们看看它是如何工作的;

            Object.prototype.compare = function(o){
              var ok = Object.keys(this);
              return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
            };
            var myArray = [
                {"name":"aaa","key":"1"},
                {"name":"bbb","key":"2"},
                {"name":"aaa","key":"1"},
                {"name":"ccc","key":"3"},
                {"name":"bbb","key":"2"}
            ],
            filtered = myArray.reduce((p,c) => p.findIndex(f => c.compare(f)) == -1 ? p.concat(c) : p,[]);
            console.log(JSON.stringify(filtered));

            注意:Object.prototype.compare() v.01 目前不会进行深度对象比较。

            【讨论】:

              【解决方案7】:
              var uniqueList = _.uniq($scope.array, function (item) {
                          return item.key;
               });
               console.log(uniqueList);
              

              使用 underscore.js,任何人都可以帮忙,所以稍后再发布。

              【讨论】:

                猜你喜欢
                • 2021-07-29
                • 2017-07-20
                • 2016-02-19
                • 2021-05-01
                • 1970-01-01
                • 2014-03-23
                相关资源
                最近更新 更多