Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.filter for specific tag in array

My array:

{
  "items": [{
    "url": "XXX",
    "title": "XXX",
    "tags": [
      "One",
      "Two"
    ]
  }]
}

I am trying to filter out and return items only where tags has "Two"

const loadArticles = async () => {
fetch(mediumRssFeed, { headers: { Accept: "application/json" } })
.then((res) => res.json())
.then((data) => data.items.filter((item) => ???))
.then((newArticles) => newArticles.slice(0, MAX_ARTICLES))
.then((articles) => setArticles(articles))
.catch((error) => console.log(error));
};

I assume it is this line:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.then((data) => data.items.filter((item) => ???))

How do I revise so that it only return those items with a tag of "Two"?

>Solution :

You can use the Array.prototype.filter() method to filter out items that do not have the tag "Two". You can use the Array.prototype.includes() method to check if the tags array includes "Two".

You can revise the line like this:

.then((data) => data.items.filter((item) =>
item.tags.includes("Two")))

This filters the items array and only keeps the items that have "Two" in their tags array.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading