I have an array of objects like this: If content tags have a value, it should be filter from another array.
let mainArray = [
{
"contentTitle": "Article 2 Test",
"contentTags": [
"Movies",
"Stories"
],
"contentPagePath": "global/en/top-page/listing-page/article2.html"
},
{
"contentTitle": "Sub Article page",
"contentTags": [
"test",
"Podcasts"
],
"contentPagePath": "global/en/top-page/listing-page/article1/sub-article-page.html"
},
{
"contentTitle": "Page title article",
"contentTags": [
"books",
"hotels"
],
"contentPagePath": "global/en/top-page/listing-page/article1.html"
},
{
"contentTitle": "Article 5",
"contentTags": [
"random",
"random2"
],
"contentPagePath": "global/en/top-page/listing-page/article-new5.html"
}];
Now I have another array from which I want to filter the above array.
let filterArray = ["Movies", "Podcasts"];
Now I want the result from the above array's keys contentTags have any of the value from 2nd array.
so After filter Result should be.
[
{
"contentTitle": "Article 2 Test",
"contentTags": [
"Movies",
"Stories"
],
"contentPagePath": "global/en/top-page/listing-page/article2.html"
},
{
"contentTitle": "Sub Article page",
"contentTags": [
"test",
"Podcasts"
],
"contentPagePath": "global/en/top-page/listing-page/article1/sub-article-page.html"
}
]
Result is like this because the mainArray have 2 objects, one have Movies from filter object and 2nd have Podcasts from filteredobject In it.
items.filter(item => item.contentTags.some(tag => query.includes(tag)))wherequeryis your array of tags to filter.