javascript 从数组中删除数组 [英] javascript remove array from array
本文介绍了javascript 从数组中删除数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有以下数组:
a = [1, 2, 3, 4, 5]
和
b = [2, 3]
如何从 a 中减去 b?所以我们有 c = a - b
应该等于 [1, 4, 5]
.jQuery 解决方案也可以.
How can I subtract b from a? So that we have c = a - b
which should be equal to [1, 4, 5]
. jQuery solution would also be fine.
推荐答案
假设您使用的浏览器具有 Array.prototype.filter
和 Array.prototype.indexOf
,你可以使用这个:
Assuming you're on a browser that has Array.prototype.filter
and Array.prototype.indexOf
, you could use this:
var c = a.filter(function(item) {
return b.indexOf(item) === -1;
});
如果有问题的浏览器没有这些方法,您可以填充它们.
If the browser in question does not have those methods, you may be able to shim them.
这篇关于javascript 从数组中删除数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文