从 JSON 过滤唯一值 [英] Filter unique values from a JSON

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

问题描述

我的页面使用 angularjs.我想从 JSON 对象中过滤值,以便不存在冗余.但是我没有找到任何方法从 angular ng-repeat 中获取唯一值.有没有办法做到这一点?

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

result = [{_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 没有重复的元素,这样我的输出就会像这样

result = [{_id: "500004",主题:完成任务",日期:2013 年 8 月 25 日"},{_id: "500005",主题:参加活动",日期:2013 年 1 月 2 日"},{_id: "500065",主题:一些任务截止日期",日期:2013 年 9 月 20 日"}]

解决方案

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

源可以在这里找到.

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

//身体在这里

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天全站免登陆