Javascript javascript从数组中删除数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7669555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript remove array from array
提问by David Weng
Assume we have the following arrays:
假设我们有以下数组:
a = [1, 2, 3, 4, 5]
and
和
b = [2, 3]
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.
如何从 a 中减去 b?所以我们有c = a - b
which 应该等于[1, 4, 5]
。jQuery 解决方案也可以。
回答by icktoofay
Assuming you're on a browser that has Array.prototype.filter
and Array.prototype.indexOf
, you could use this:
假设您使用的是具有Array.prototype.filter
和的浏览器Array.prototype.indexOf
,您可以使用它:
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.
如果有问题的浏览器没有这些方法,您可以填充它们。
回答by jfriend00
For code that would work in all browsers, you would have to manually find each element from b in a and remove it.
对于适用于所有浏览器的代码,您必须手动从 a 中的 b 中查找每个元素并将其删除。
var a = [1, 2, 3, 4, 5];
var b = [2, 3];
var result = [], found;
for (var i = 0; i < a.length; i++) {
found = false;
// find a[i] in b
for (var j = 0; j < b.length; j++) {
if (a[i] == b[j]) {
found = true;
break;
}
}
if (!found) {
result.push(a[i]);
}
}
// The array result now contains just the items from a that are not in b
Working example here: http://jsfiddle.net/jfriend00/xkBzR/
这里的工作示例:http: //jsfiddle.net/jfriend00/xkBzR/
And, here's a version that could be faster for large arrays because it puts everything into an object for hashed lookups rather than brute force array searching:
而且,这是一个对于大型数组可能更快的版本,因为它将所有内容放入一个对象中以进行散列查找而不是蛮力数组搜索:
var a = [1, 2, 3, 4, 5];
var b = [2, 3];
function filterArray(src, filt) {
var temp = {}, i, result = [];
// load contents of filt into object keys for faster lookup
for (i = 0; i < filt.length; i++) {
temp[filt[i]] = true;
}
// go through src
for (i = 0; i < src.length; i++) {
if (!(src[i] in temp)) {
result.push(src[i]);
}
}
return(result);
}
var filtered = filterArray(a, b);
Working example here: http://jsfiddle.net/jfriend00/LUcx6/
这里的工作示例:http: //jsfiddle.net/jfriend00/LUcx6/
回答by Mohammad Usman
This is a modified version of the answer posted by @icktoofay.
这是@icktoofay 发布的答案的修改版本。
In ES6 we can make use of:
在 ES6 中,我们可以使用:
This will simplify our code to:
这将简化我们的代码:
var c = a.filter(x => !b.includes(x));
Demo:
演示:
var a = [1, 2, 3, 4, 5];
var b = [2, 3];
var c = a.filter(x => !b.includes(x));
console.log(c);
回答by Natividad Lara Diaz
For the ones struggling with Objects, like Date, you'll find out that two different objects are never equal to each other, even if they have the same values, so the answers above wouldn't work. Here is an answer to this problem in ES6.
对于那些在对象上苦苦挣扎的人,比如 Date,你会发现两个不同的对象永远不会彼此相等,即使它们具有相同的值,所以上面的答案不起作用。这是 ES6 中这个问题的答案。
const c = a.filter(aObject => b.findIndex(bObject => aObject.valueOf() === bObject.valueOf()) === -1)
回答by Kakashi
Here an implementation for try works in all browsers:
这里的 try 实现适用于所有浏览器:
if('filter' in Array == false) {
Array.prototype.filter =
function(callback) {
if(null == this || void 0 == this) {
return;
}
var filtered = [];
for(i = 0, len = this.length; i < len; i++) {
var tmp = this[i];
if(callback(tmp)) {
filtered.push(tmp);
}
}
return filtered;
}
}
a = [1, 2, 3, 4, 5];
b = [2, 3];
var c = a.filter(function(item) { /*implementation of icktoofay */
return b.indexOf(item) === -1;
});