Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  140] [ 7]  / answers: 1 / hits: 32225  / 9 Years ago, mon, september 7, 2015, 12:00:00

I have an array and i want to remove a record from it i have use Array.filter() but its return same array as it is.



My Code:



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));

More From » jquery

 Answers
17

You should return true or false in the filter in order to filter data from an array.

Return true to add the element in the filtered list, false otherwise.

So you can do something like this using filter()


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

[#65160] Friday, September 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;