How do I filter an array of object containing an array of object itself?

女生的网名这么多〃 提交于 2021-02-11 13:01:44

问题


I have an array of articles that are linked to some tags via a many to many system. When I want to get all my articles, the JSON that comes back looks as follows:

[
    {
        "id": 1,
        "title": "Subin",
        "content": "Integer ac leo...",
        "illustration": "http://dummyimage.com/1920x1080.png/ff4444/ffffff",
        "lang": "fr",
        "tags": [
            {
                "name": "project",
                "description": "Praesent id massa...",                   
                "slug": "854963934-6",
                "id": 4,
            },
            {
                "name": "Grass-roots",
                "description": "Proin eu mi...",
                "slug": "528521892-6",
                "id": 2,
            }
        ]
    },
    {
        "id": 2,
        "title": "Voyatouch",
        "content": "Curabitur gravida nisi at nibh...",
        "illustration": "http://dummyimage.com/1920x1080.png/cc0000/ffffff",
        "lang": "fr",
        "tags": [
            {
                "name": "Grass-roots",
                "description": "Proin eu mi...",
                "slug": "528521892-6",
                "id": 2,
            },
            {
                "name": "User-friendly",
                "description": "Vestibulum quam sapien...",
                "slug": "237872269-9",
                "id": 1,
            }
        ]
    },
]

I would like to filter the articles by their tags. If I click on a tag, then all the articles having this tag keeps showing up while the other disappear.

Normally, I would do the following if the tags were just an array of strings:

filter (tag) {
  // This is a VueJS context
  return this.articles.filter(article => article.tag === tag)
}

However, since it's an array of object, I tried to do the following:

filter (tag) {
  let self = this
  return this.articles.filter(article => {
    return article.tags.filter(tag => tag.name === self.selected)
  })
}

But it returns nothing.

What would the correct method be?

Thank you in advance


回答1:


You can use filter() to filter the array. Use some() to check if the article has a certain tag.

var arr = [{"id":1,"title":"Subin","content":"Integer ac leo...","illustration":"http://dummyimage.com/1920x1080.png/ff4444/ffffff","lang":"fr","tags":[{"name":"project","description":"Praesent id massa...","slug":"854963934-6","id":4},{"name":"Grass-roots","description":"Proin eu mi...","slug":"528521892-6","id":2}]},{"id":2,"title":"Voyatouch","content":"Curabitur gravida nisi at nibh...","illustration":"http://dummyimage.com/1920x1080.png/cc0000/ffffff","lang":"fr","tags":[{"name":"Grass-roots","description":"Proin eu mi...","slug":"528521892-6","id":2},{"name":"User-friendly","description":"Vestibulum quam sapien...","slug":"237872269-9","id":1}]}]

var tagName = "project"; //Tag name to search
var result = arr.filter(o => o.tags.some(x => x.name === tagName));;

console.log(result);

Doc: .filter(), .some()



来源:https://stackoverflow.com/questions/53087369/how-do-i-filter-an-array-of-object-containing-an-array-of-object-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!