Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove an item in an array of object where key value is

I have the following object of arrays:

{
    "Custom Mexico": [
        {
            "cursor": "eyJsYXNasdf0LCJsYXN0X3ZhbHVlIjoiQWd1YXNjYWxpZW50ZXMgTGljZW5zZSBQbGF0ZSBIYXQifQ==",
            "node": {
                "id": "gid://shopify/Product/993sasd5454",
                "title": "Aguascalientes License Plate Hat"
            }
        },
        {
            "cursor": "eyJsYXN0X2lkIasdfYWx1ZSI6IkJhamEgQ2FsaWZvcm5pYSBMaWNlbnNlIFBsYXRlIEhhdCJ9",
            "node": {
                "id": "gid://shopify/Product/18asdf9144",
                "title": "Baja California License Plate Hat"
            }
        },
        {
            "cursor": "eyJsYXN0X2asdf3MTYwLCJsYXN0X3ZhbHVlIjoiQmFqYSBDYWxpZm9ybmlhIFN1ciBMaWNlbnNlIFBsYXRlIEhhdCJ9",
            "node": {
                "id": "gid://shopify/Product/13asdf67160",
                "title": "Baja California Sur License Plate Hat"
            }
        }
    ],
    "Mexico Stuff": [
        {
            "cursor": "eyJsYXN0asdfjQxNTExNTEyLCJsYXN0X3ZhbHVlIjoiMCJ9",
            "node": {
                "id": "gid://shopify/Product/73sdf11512",
                "title": "Tamaulipas Metal License Plate"
            }
        },
        {
            "cursor": "eyJsYXN0X2lkIjasdfNzQ0LCJsYXN0X3ZhbHVlIjoiMCJ9",
            "node": {
                "id": "gid://shopify/Product/736asdf78744",
                "title": "Hidalgo Metal License Plate"
            }
        },
        {
            "cursor": "eyJsYXNasdfA4LCJsYXN0X3ZhbHVlIjoiMCJ9",
            "node": {
                "id": "gid://shopify/Product/736asdf08",
                "title": "Guanajuato Metal License Plate"
            }
        }
    ]
}

My goal is to return the exact same structure but removing an item if it’s in the array:

// where products is the object above:
  const item = "gid://shopify/Product/13s345f67160";
  const listCopy = structuredClone(products);

  const updatedList = Object.keys(listCopy).map(elGroup => {
    return listCopy[elGroup].filter(obj => obj.node.id != item);
  })

This returns the array but omits the name of the Object in example "Custom Mexico".
How can I accomplish this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could directly mutate the copy and use that afterward.

for (const [k, v] of Object.entries(listCopy))
    listCopy[k] = v.filter(obj => obj.node.id != item);
// use listCopy now

Alternatively, filter the entries and use Object.fromEntries to build the object again. There is no need to make a copy with structuredClone in this case.

const updatedList = Object.fromEntries(
    Object.entries(products).map(([k, v]) => [k, v.filter(obj => obj.node.id != item)])
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading