I have the two following Arrays of objects.
const respProducts = [
{
"cursor": "eyJsYXN0X2lkIjo4OT234234sYXN0X3ZhbHVlIjo4OTkzOTgyMjExfQ==",
"node": {
"id": "gid://shopify/Product/8923422211",
"title": "California Here"
}
},
{
"cursor": "eyJsYXN0X2lkIjo5234234sYXN0X3ZhbHVlIjo5MDExNDM0MzA3fQ==",
"node": {
"id": "gid://shopify/Product/923423434307",
"title": "Texas 2000 Here"
}
},
{
"cursor": "eyJsYXN0X2lkIj234234LCJsYXN0X3ZhbHVlIjo5MzM4MDczODcwfQ==",
"node": {
"id": "gid://shopify/Product/23423470",
"title": "Texas Black Here"
}
}
]
const currentTemplates = {
"Texas 2000 Here": {
"productTemplate": {
},
"colorVariants": false
},
"Alabama": {
"productTemplate": {
},
"colorVariants": false
},
"Alaska": {
"productTemplate": {
},
"colorVariants": false
},
"Arizona": {
"productTemplate": {
},
"colorVariants": false
}
}
I need to check if an item from respProducts is found in currentTemplates if so do not return it if it is found then return it. I need respProducts to retain it’s structure.
Only remove any items that are in currentTemplates.
My attempt:
const respBuildArray = respProducts.filter(el => el.node.title.includes(Object.keys(currentTemplates).map(el => el)));
In the above example I would like to get back:
const respBuildArray = [
{
"cursor": "eyJsYXN0X2lkIjo4OT234234sYXN0X3ZhbHVlIjo4OTkzOTgyMjExfQ==",
"node": {
"id": "gid://shopify/Product/8923422211",
"title": "California Here"
}
},
{
"cursor": "eyJsYXN0X2lkIj234234LCJsYXN0X3ZhbHVlIjo5MzM4MDczODcwfQ==",
"node": {
"id": "gid://shopify/Product/23423470",
"title": "Texas Black Here"
}
}
]
since Texas 2000 Here is in currentTemplates.
My above code returns an empty array.
2
Answers
There is no need for the
map()
. And it seems like you want to negate (!
) theincludes()
You can use the
in
operator to check if the string exists as a property name (key) incurrentTemplates
and use that as criteria for filtering. This avoids creating an unneeded array of keys: