javascript中数组中删除阵列 [英] javascript remove array from array

查看:116
本文介绍了javascript中数组中删除阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下的数组:

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和<一个href=\"https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf\"><$c$c>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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆