skip to Main Content

I have this array and I’m trying to delete a site from it but it’s not working. I know the function is called because I have added an alert to it and it alerted.

I removed some functions from the post just to make it shorter. But I have one function that removes ites from the array and it works great. It’s just deleting a site by siteid that doesn’t work for some reason.

[
  {
    "siteId": "9843598",
    "items": [
      {
        "catNum": 9418956196,
        "title": "This is item 1",
        "qty": 1
      }
    ]
  },
  {
    "siteId": "89419111",
    "items": [
      {
        "catNum": 1231654351,
        "title": "This is item 2",
        "qty": 1
      },
      {
        "catNum": 9418956196,
        "title": "This is item 1",
        "qty": 1
      }
    ]
  }
]
function deleteSite(quoteCart, siteId) {
  try {
    quoteCart = quoteCart.filter((site) => site.siteId !== siteId);
    console.log("Filtered array:", quoteCart); // Check the filtered array

    updateCartDisplay();
    openWindowWithArrayContents();
  } 
  catch (error) {
    console.error("Error in deleteSite:", error);
  }
}

If I pressed delete on site # 9843598 I would expect the

[
  {
    "siteId": "89419111",
    "items": [
      {
        "catNum": 9418956196,
        "title": "This is item 1",
        "qty": 1
      },
      {
        "catNum": 1231654351,
        "title": "This is item 2",
        "qty": 1
      }
    ]
  }
]

Playground
https://jsfiddle.net/Bigmuddyfoot/4v5w2nLa/

3

Answers


  1. Hello, since your codes are unrelated, I wrote it this way so that you can implement it in your project. I hope it works for you.

    const data = [
      {
        "siteId": "9843598",
        "items": [
          {
            "catNum": 9418956196,
            "title": "This is item 1",
            "qty": 1
          }
        ]
      },
      {
        "siteId": "89419111",
        "items": [
          {
            "catNum": 1231654351,
            "title": "This is item 2",
            "qty": 1
          },
          {
            "catNum": 9418956196,
            "title": "This is item 1",
            "qty": 1
          }
        ]
      },
      {
        "siteId": "9843598",
        "items": [
          {
            "catNum": 9418956196,
            "title": "This is item 3",
            "qty": 1
          }
        ]
      }
    ];
    
    function removeDuplicateSiteIds(data) {
      try {
        const seenSiteIds = new Set();
    
        for (let i = 0; i < data.length; i++) {
          const siteId = data[i].siteId;
    
          if (seenSiteIds.has(siteId)) {
            // Duplicate siteId found, remove the item
            const removedItem = data.splice(i, 1)[0];
            i--; // Adjust the index as we removed an element
            console.log(`Removed duplicate siteId: ${siteId}`, removedItem);
          } else {
            seenSiteIds.add(siteId);
          }
        }
      } catch (error) {
        console.error("An error occurred while removing duplicate siteIds:", error.message);
      }
    }
    
    // Example usage
    console.log("Before removal:", data);
    
    removeDuplicateSiteIds(data);
    
    console.log("After removal:", data);
    
    Login or Signup to reply.
  2. var cart=[
    
    
    {
        "siteId": "9843598",
        "items": [
          {
            "catNum": 9418956196,
            "title": "This is item 1",
            "qty": 1
          }
        ]
      },
      {
        "siteId": "89419111",
        "items": [
          {
            "catNum": 1231654351,
            "title": "This is item 2",
            "qty": 1
          },
          {
            "catNum": 9418956196,
            "title": "This is item 1",
            "qty": 1
          }
        ]
      }
    ]
    function deleteSite(quoteCart, siteId) {
      try {
        quoteCart = quoteCart.filter((site) => site.siteId !== siteId);
        console.log("Filtered array:", quoteCart); // Check the filtered array
    
        //updateCartDisplay();
        //openWindowWithArrayContents();
      } 
      catch (error) {
        console.error("Error in deleteSite:", error);
      }
    }
    deleteSite(cart, (89419111).toString())
    
    Login or Signup to reply.
  3. I think the deleteSite function might not be updating the cart properly due to a scoping issue. When you’re passing quoteCart into the deleteSite function, the modifications made inside the function are limited to the local scope. So, you could return the updated quoteCart from the deleteSite function and use that returned value.

    Check the modified version:

    function deleteSite(quoteCart, siteId) {
        try {
            // Remove the site with the specified siteId
            quoteCart = quoteCart.filter((site) => site.siteId !== siteId);
            console.log("Filtered array:", quoteCart); // Check the filtered array
    
            // If there's only one site left and it matches the siteId, clear the quoteCart
            if (quoteCart.length === 1 && parseInt(quoteCart[0].siteId) === siteId) {
                quoteCart = [];
            }
    
            return quoteCart; // Return the updated quoteCart
        } catch (error) {
            console.error("Error in deleteSite:", error);
            return quoteCart; // Return the original quoteCart in case of an error
        }
    }
    

    And when calling the deleteSite function, update the quoteCart variable and refresh the display:

    quoteCart = deleteSite(quoteCart, siteIdToDelete);
    updateCartDisplay();
    

    See this JS<>Fiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search