10

我正在为我的页面使用 angularjs。我想从 JSON 对象中过滤值,因此不存在冗余。但我没有找到任何方法从 angular 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"
        }
]
4

3 回答 3

17

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

来源可以在这里找到。

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

<div ng-repeat="item in result | unique:'_id'">
    //Body here
</div>
于 2013-09-11T17:58:50.423 回答
5

您可以在 angular.filter 模块(https://github.com/a8m/angular-filter) 中使用 'unique'(aliases: uniq) 过滤器

用法: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
bar 20
baz 30

于 2014-07-21T05:17:17.150 回答
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/

于 2016-04-22T17:34:55.310 回答