从JSON筛选唯一值 [英] Filter unique values from a JSON

查看:214
本文介绍了从JSON筛选唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用angularjs为我的网页。我想从JSON对象过滤值,所以没有冗余的存在。但我没有找到任何方式来获得角NG-重复的唯一值。反正有做到这一点?

好吧,下面是一些关于这个问题的一些描述。我在这个格式的JSON。这JSON我从服务获得。因此,我们不能期望重复的数据如何occure。

 的结果= [
        {
            _id:500004,
            主题:完成任务,
            日期:2013年8月25日
        },
        {
            _id:500004,
            主题:完成任务,
            日期:2013年8月25日
        },
        {
            _id:500005,
            主题:出席事件
            日期:2013年1月2日
        },
        {
            _id:500065,
            主题:一些任务的最后期限
            日期:2013年9月20日
        },
        {
            _id:500004,
            主题:完成任务,
            日期:2013年8月25日
        }
]

我要输出JSON是没有重复的元素,让自己的输出是这样

 的结果= [
        {
            _id:500004,
            主题:完成任务,
            日期:2013年8月25日
        },
        {
            _id:500005,
            主题:出席事件
            日期:2013年1月2日
        },
        {
            _id:500065,
            主题:一些任务的最后期限
            日期:2013年9月20日
        }
]


解决方案

您可以使用角UI中有定义的唯一过滤器。

源可以发现<一个href=\"https://github.com/angular-ui/angular-ui/blob/master/modules/filters/unique/unique.js\">here.

基本上,你就可以利用过滤器如下:

 &LT; D​​IV NG重复=,在结果项|独特:'_身份证'&GT;
    //这里车身
&LT; / DIV&GT;

I am using angularjs for my page. I want to filter the values from the JSON object, So that no redundancy exists. But I didn't find any way to get the unique values from angular ng-repeat. Is there anyway to do that?

Ok, Here is some description about the question. I have a JSON in this format. This JSON I am getting from a service. So we cant expect how the repeated data occure.

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"
        }
]

I want the output JSON to be with no repeated elements, So that my output will be something like this

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"
        }
]

解决方案

You can make use of Angular UI which has the unique filter defined.

The source can be found here.

Basically, you can then make use of the filter as follows:

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

这篇关于从JSON筛选唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆