1

I am trying to filter a sub array of a parent array and my results are coming back empty. I am trying to find matches to the color_family.

My array looks like:

const arr =[
  {
    "id": 123,
    "acf": {
      "product_colors": [
        {
          "color_family": "grey"
        },
        {
          "color_family": "taupe"
        }
      ]
    }
  },
  {
    "id": 456,
    "acf": {
      "product_colors": [
        {
          "color_family": "red"
        },
        {
          "color_family": "taupe"
        }
      ]
    }
  }
]

What I am filtering on is

const findColors = ["grey", "taupe"]

What I have tried with no luck is

const res = arr.filter(
  x => x.acf.product_colors.find(
    color_family => findColors.includes(color_family)
  )
)

This is returning no results when it should return 2 results. Can someone point me in the right direction?

2
  • I think you have a typo x.acf.product_solors.find( ==> x.acf.product_colors.find( Commented Mar 9, 2019 at 0:13
  • It was just a typo in the question. My code does not have the typo. Commented Mar 9, 2019 at 0:15

1 Answer 1

3

In addition to the typo, the param to the find argument is an object with color_family:

const res = arr.filter(x => x.acf.product_colors.find(col => {
  return findColors.includes(col.color_family);
}))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.