How to filter out an array of strings?

I’m trying to filter an array of objects where a certain key in the object contains an array of strings. Here’s an example of the data structure.

let array = [{
  tags: ["this is a tag"]
  tags: ["this is not a tag"]

I need to filter this array based on certain criteria. Here’s what I’ve started with.

const filtered = array.filter(entry => entry["tags"].includes("n"))

This doesn’t return anything but the following does.

const filtered = array.filter(entry => entry["tags"].includes("this is a tag"))

This returns the first entry because the entirety of the string matches. What I want is for comparisons between partial strings instead of the whole string but I can’t seem to get anything to work. Does anyone know how to compare string arrays such that the first example would return the second entry?


Your includes is checking if the array ["this is a tag"] contains the string "n", which it clearly doesn’t.

If you’re looking to check if an array contains a string containing a specific letter, you need to do a deeper search:

let array = [{
  tags: ["this is a tag"]
}, {
  tags: ["this is not a tag"]

const filtered = array.filter(entry => entry.tags.some(tag => tag.includes("n")))


Also note how I replaced the entry["tags"] with entry.tags. Bracket access isn’t necessary there.

Source: stackoverflow