Array.filter is not working properly

I have an array and I want to remove the entry from it. I use Array.filter() , but it returns the same array as it.

My code is:

 var url = window.location.pathname, orderId = url.split('/').slice(-2)[0]; var Cart = JSON.parse(localStorage.getItem('Cart')); newCart=Cart.filter(function(item) { if (parseInt(item.orderId) == parseInt(orderId)) { return {}; } else { return item; } }); localStorage.setItem('Cart',JSON.stringify(newCart)); 
+7
javascript jquery arrays
source share
1 answer

You need to return true or false to the filter to filter the data from the array, return true to save the element, otherwise false. This way you can do something like this using filter()

 newCart = Cart.filter(function(item) { return parseInt(item.orderId, 10) != parseInt(orderId, 10); }); 
+12
source share

All Articles