How Do I Filter An Array Of Object Containing An Array Of Object Itself?
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: [ { '
Solution 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 searchvar result = arr.filter(o => o.tags.some(x => x.name === tagName));;
console.log(result);
Post a Comment for "How Do I Filter An Array Of Object Containing An Array Of Object Itself?"