从JSON中过滤唯一值

时间:2013-09-11 17:29:29

标签: angularjs filter unique unique-key ng-repeat

我正在为我的页面使用angularjs。我想过滤JSON对象中的值,因此不存在冗余。但我没有找到任何方法来获得角度ng-repeat的唯一值。反正有吗?

好的,这是关于这个问题的一些描述。我有这种格式的JSON。这是我从服务获得的JSON。所以我们不能指望重复数据是如何发生的。

result = [
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        },      
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        },      
        {
            _id: "500005",
            subject: "Attend to the event",
            date: "2 Jan, 2013"
        },      
        {
            _id: "500065",
            subject: "Some task deadline",
            date: "20 Sep, 2013"
        },      
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        }
]

我希望输出JSON没有重复的元素,所以我的输出将是这样的

result = [
        {
            _id: "500004",
            subject: "Complete the task",
            date: "25 Aug, 2013"
        },      
        {
            _id: "500005",
            subject: "Attend to the event",
            date: "2 Jan, 2013"
        },      
        {
            _id: "500065",
            subject: "Some task deadline",
            date: "20 Sep, 2013"
        }
]

3 个答案:

答案 0 :(得分:17)

您可以使用已定义unique过滤器的Angular UI。

可以找到来源here

基本上,您可以按如下方式使用过滤器:

<div ng-repeat="item in result | unique:'_id'">
    //Body here
</div>

答案 1 :(得分:5)

您可以在angular.filter模块中使用&#39; unique&#39;(别名:uniq)过滤器(https://github.com/a8m/angular-filter

用法:colection | uniq: 'property'
您可以按嵌套属性过滤到:colection | uniq: 'property.nested_property'

所以你可以做那样的事情..

function MainController ($scope) {
 $scope.orders = [
  { id:1, customer: { name: 'foo', id: 10 } },
  { id:2, customer: { name: 'bar', id: 20 } },
  { id:3, customer: { name: 'foo', id: 10 } },
  { id:4, customer: { name: 'bar', id: 20 } },
  { id:5, customer: { name: 'baz', id: 30 } },
 ];
}

HTML:我们根据客户ID进行过滤,即删除重复的客户

<th>All customers list: </th>
<tr ng-repeat="order in orders | unique: 'customer.id'" >
   <td> {{ order.customer.name }} , {{ order.customer.id }} </td>
</tr>

<强>结果: 所有客户列表:
foo 10
酒吧20
巴兹30

答案 2 :(得分:3)

var data = [
    {
        _id: "500004",
        subject: "Complete the task",
        date: "25 Aug, 2013"
    },      
    {
        _id: "500004",
        subject: "Complete the task",
        date: "25 Aug, 2013"
    },      
    {
        _id: "500005",
        subject: "Attend to the event",
        date: "2 Jan, 2013"
    },      
    {
        _id: "500065",
        subject: "Some task deadline",
        date: "20 Sep, 2013"
    },      
    {
        _id: "500004",
        subject: "Complete the task",
        date: "25 Aug, 2013"
    }
]

var uniqueNames = [];
var uniqueObj = [];

for(i = 0; i< data.length; i++){    

    if(uniqueNames.indexOf(data[i]._id) === -1){
        uniqueObj.push(data[i])
        uniqueNames.push(data[i]._id);        
    }       

}

console.log('uniqueObj',uniqueObj)

http://jsfiddle.net/C97DJ/165/