[英]Array.filter is not working properly
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.我有一个数组,我想从中删除一条记录,我使用了
Array.filter()
但它返回的数组与原来相同。
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));
You should return true
or false
in the filter in order to filter data from an array.您应该在过滤器中返回
true
或false
以过滤数组中的数据。
Return true
to add the element in the filtered list, false
otherwise.返回
true
将元素添加到过滤列表中,否则返回false
。
So you can do something like this using filter()
所以你可以使用
filter()
做这样的事情
newCart = Cart.filter(function(item) {
return parseInt(item.orderId, 10) != parseInt(orderId, 10);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.