Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

angularjs - Filter unique values from a JSON

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"
        }
]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...