在 JavaScript 中计算两个数组的交集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16227197/
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
Compute intersection of two arrays in JavaScript
提问by Justin
Given two arrays of unequal length:
给定两个长度不等的数组:
var arr1 = ["mike", "sue", "tom", "kathy", "henry"]; //arr1.length = 5
var arr2 = ["howey", "jim", "sue", "jennifer", "kathy", "hank", "alex"]; //arr2.length = 7
How can I find the values common to both arrays? In this case "sue"
and "kathy"
should be returned.
如何找到两个数组共有的值?在这种情况下"sue"
,"kathy"
应该返回。
回答by Paul S.
Here is an intersection function based on Array.prototype.filter
这是一个基于的交集函数 Array.prototype.filter
function intersect(a, b) {
var t;
if (b.length > a.length) t = b, b = a, a = t; // indexOf to loop over shorter
return a.filter(function (e) {
return b.indexOf(e) > -1;
});
}
var arr1 = ["mike", "sue", "tom", "kathy", "henry"];
arr2 = ["howey", "jim", "sue", "jennifer", "kathy", "hank", "alex"];
intersect(arr1, arr2); // ["sue", "kathy"]
You might also want to consider the following
您可能还需要考虑以下内容
var arr1 = ['sue', 'sue', 'kathy'],
arr2 = ['kathy', 'kathy', 'sue'];
The above would now give ["sue", "sue", "kathy"]
. If you don't want duplicates you could do a further filter on this. This would also standardise results. i.e.
以上现在会给["sue", "sue", "kathy"]
. 如果您不想重复,您可以对此进行进一步过滤。这也将标准化结果。IE
return a
.filter(/* .. */) // same as before
.filter(function (e, i, c) { // extra step to remove duplicates
return c.indexOf(e) === i;
});
Adding this will now return the same result as the previous arrays (["sue", "kathy"]
), even though there were duplicates.
添加这个现在将返回与之前的数组 ( ["sue", "kathy"]
)相同的结果,即使有重复。
回答by xdazz
You could use Array.filter:
你可以使用Array.filter:
var result = arr1.filter(function(n) {
return arr2.indexOf(n) > -1;
});
回答by alex
You want to find the intersection of two arrays?
你想找到两个数组的交集吗?
You could use Underscore's intersection()
. This will give you a list of values present in both arrays.
您可以使用 Underscore 的intersection()
. 这将为您提供两个数组中存在的值列表。
var commonValues = _.intersection(arr1, arr2);
If you didn't want to use a library, it'd be trivial to implement...
如果您不想使用库,那么实现就很简单了...
var commonValues = arr1.filter(function(value) {
return arr2.indexOf(value) > -1;
});
If Array.prototype.filter()
and Array.prototype.indexOf()
are not supported in your target platforms...
如果您的目标平台不支持Array.prototype.filter()
和Array.prototype.indexOf()
不支持...
var commonValues = [];
var i, j;
var arr1Length = arr1.length;
var arr2Length = arr2.length;
for (i = 0; i < arr1Length; i++) {
for (j = 0; j < arr2Length; j++) {
if (arr1[i] === arr2[j]) {
commonValues.push(arr1[i]);
}
}
}
回答by Blender
Iterate over one of the arrays and compare the objects with the other:
迭代其中一个数组并将对象与另一个进行比较:
var results = [];
for (var i = 0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) !== -1) {
results.push(arr1[i]);
}
}