0

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.

2
  • "Now I want the result" - please share a minimal reproducible example of your effort so far Commented Oct 19, 2022 at 12:33
  • the naive approach is items.filter(item => item.contentTags.some(tag => query.includes(tag))) where query is your array of tags to filter. Commented Oct 19, 2022 at 12:38

2 Answers 2

3

You can use filter with some to achieve that goal

const 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"}];

const filterArray = ["Movies", "Podcasts"];

const result = mainArray.filter(item => item.contentTags.some(contentTag => filterArray.includes(contentTag)))

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a combination of filter and find to achieve the above scenario:

contents.filter((d) => d.contentTags.find(e => targetTags.indexOf(e) > -1))

Here I am applying filter on the contents array which is your source array and it will return the objects which only passes the find logic which I have return. Inner find will loop the contentTags and check if it exists in your comparison tags array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.