使用angular js从json数组中删除重复值

时间:2016-06-13 10:30:13

标签: javascript arrays angularjs json

我想删除以下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"} 
];

7 个答案:

答案 0 :(得分:0)

您可以使用唯一过滤器。

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

答案 1 :(得分:0)

您可以使用 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);

答案 2 :(得分:0)

你可以使用纯粹的javascript!

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

this answer !!

复制了它

在你的情况下:

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

答案 3 :(得分:0)

您可以使用

&#13;
&#13;
$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);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

 $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;
    }

答案 5 :(得分:0)

纯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目前不会进行深层对象比较。

答案 6 :(得分:0)

var uniqueList = _.uniq($scope.array, function (item) {
            return item.key;
 });
 console.log(uniqueList);

使用 underscore.js,任何人帮忙,稍后发布。