javascript中数组中删除阵列 [英] javascript remove array from array
问题描述
假设我们有以下的数组:
Assume we have the following arrays:
a = [1, 2, 3, 4, 5]
和
b = [2, 3]
我怎样才能从减去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.
推荐答案
假设你在有<一个浏览器href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter\"><$c$c>Array.prototype.filter$c$c>和<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf\"><$c$c>Array.prototype.indexOf$c$c>,你可以使用这样的:
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屋!