简体   繁体   中英

Filter by elements in an Array in AngularJS

I have a JSON File with Tasks. They are structured as follows:

[
{
    "id":"1",
    "name":"task1",
    "tags":["tag1","tag2","tag3"]
},
{
    "id":"2",
    "name":"task2",
    "tags":["tag4","tag2","tag5"]
},
{
    "id":"3",
    "name":"task3",
    "tags":["tag3"]
}
]

Here's a Plunkr for what I want http://plnkr.co/edit/kMhiHDyybHYxGfV7W39z?p=preview

Basically, this is what I want : http://jsfiddle.net/TahmidTanzim/N9Vqk/

I've looked through various SO questions and they all suggest I am doing it right. However I don't understand where I am going wrong. I want only those tasks that contain the selectedTag to be displayed in the red list.

Thanks!

Just change filters to compare inner tags using arrays.

myapp.filter('tagFilter',function()
{
    return function(Data,selectedTags)
    {
        if(selectedTags.length===0) return Data;
        var tempData=[];
        for(var i  in Data) {
            for(var z in Data[i].tags) {
                for(var k in selectedTags) {
                    var value = selectedTags[k];
                    if(value == Data[i].tags[z]) {
                        tempData.push(Data[i]);
                        break;             
                    }
                }
            }
        }
        return tempData;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM