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?
-
I think you have a typo x.acf.product_solors.find( ==> x.acf.product_colors.find(Nils Kähler– Nils Kähler2019年03月09日 00:13:44 +00:00Commented Mar 9, 2019 at 0:13
-
It was just a typo in the question. My code does not have the typo.DigitalMcGrath– DigitalMcGrath2019年03月09日 00:15:39 +00:00Commented Mar 9, 2019 at 0:15
1 Answer 1
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);
}))
answered Mar 9, 2019 at 0:15
Phix
10.1k4 gold badges39 silver badges65 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-js