skip to Main Content

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


  1. There is no need for the map(). And it seems like you want to negate (!) the includes()

    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 } } 
    
    const allTemplateTitles = Object.keys(currentTemplates);
    
    const filtered = respProducts.filter(e => !allTemplateTitles.includes(e.node.title))
    
    console.log(filtered)
    Login or Signup to reply.
  2. You can use the in operator to check if the string exists as a property name (key) in currentTemplates and use that as criteria for filtering. This avoids creating an unneeded array of keys:

    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}};
    const expected = [{"cursor":"eyJsYXN0X2lkIjo4OT234234sYXN0X3ZhbHVlIjo4OTkzOTgyMjExfQ==","node":{"id":"gid://shopify/Product/8923422211","title":"California Here"}},{"cursor":"eyJsYXN0X2lkIj234234LCJsYXN0X3ZhbHVlIjo5MzM4MDczODcwfQ==","node":{"id":"gid://shopify/Product/23423470","title":"Texas Black Here"}}];
    
    const actual = respProducts.filter(
      (p) => !(p.node.title in currentTemplates),
    );
    
    console.log(JSON.stringify(actual) === JSON.stringify(expected)); // true
    console.log(actual);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search