Javascript Javascript算法在数组中查找不在另一个数组中的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2963281/
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 algorithm to find elements in array that are not in another array
提问by Tauren
I'm looking for a good algorithm to get all the elements in one array that are not elements in another array. So given these arrays:
我正在寻找一种好的算法来获取一个数组中不是另一个数组中的元素的所有元素。所以给定这些数组:
var x = ["a","b","c","t"];
var ?????????y = [???????"d","a","t","e","g"];
I want to end up with this array:
我想以这个数组结束:
var z = ["d","e","g"];
I'm using jquery, so I can take advantage of $.each()and $.inArray(). Here's the solution I've come up with, but it seems like there should be a better way.
我正在使用 jquery,所以我可以利用$.each()和$.inArray()。这是我提出的解决方案,但似乎应该有更好的方法。
// goal is to get rid of values in y if they exist in x
var x = ["a","b","c","t"];
var y = ["d","a","t","e","g"];
var z = [];
$.each(y, function(idx, value){
if ($.inArray(value,x) == -1) {
z.push(value);
}
});
?alert(z); // should be ["d","e","g"]
Here is the code in action. Any ideas?
这是正在运行的代码。有任何想法吗?
采纳答案by Matthew Flaschen
var z = $.grep(y, function(el){return $.inArray(el, x) == -1});
Also, that method name is too short for its own good. I would expect it to mean isElementInArray, not indexOf.
此外,该方法名称太短了。我希望它表示 isElementInArray,而不是 indexOf。
For a demo with objects, see http://jsfiddle.net/xBDz3/6/
有关对象的演示,请参阅http://jsfiddle.net/xBDz3/6/
回答by Maurizio In denmark
Late answer with the new ECMA5 javascript:
新的 ECMA5 javascript 的迟到答案:
var x = ["a","b","c","t"];
var y = ["d","a","t","e","g"];
myArray = y.filter( function( el ) {
return x.indexOf( el ) < 0;
});
回答by kofifus
in ES6 simply
在 ES6 中
const x = ["a", "b", "c", "t"];
const y = ["d", "a", "t", "e", "g"];
console.log( y.filter(e => !x.includes(e)) );
(another option is y.filter(e => x.indexOf(e)===-1))
(另一种选择是y.filter(e => x.indexOf(e)===-1))
回答by Matt Woelk
Here's an alternative using underscore.js:
这是使用underscore.js的替代方法:
function inAButNotInB(A, B) {
return _.filter(A, function (a) {
return !_.contains(B, a);
});
}
回答by drawnonward
Make sorted copies of the arrays first. If the top elements are equal, remove them both. Otherwise remove the element that is less and add it to your result array. If one array is empty, then add the rest of the other array to the result and finish. You can iterate through the sorted arrays instead of removing elements.
首先制作数组的排序副本。如果顶部元素相等,则将它们都删除。否则删除较少的元素并将其添加到结果数组中。如果一个数组为空,则将另一个数组的其余部分添加到结果中并完成。您可以遍历已排序的数组而不是删除元素。
// assume x and y are sorted
xi = 0; yi = 0; xc = x.length; yc = y.length;
while ( xi < xc && yi < yc ) {
if ( x[xi] == y[yi] ) {
xi += 1;
yi += 1;
} else if ( x[xi] < y[yi] ) {
z.push( x[xi] );
xi += 1;
} else {
z.push( y[yi] );
yi += 1;
}
}
// add remainder of x and y to z. one or both will be empty.
回答by Koobz
It lets you run queries like this against javascript objects.
它允许您对 javascript 对象运行这样的查询。
For example:
例如:
var users = [ { name: "jacob", age: 25 }, { name: "bob" , age: 30 }]
var additionalusers = [ { name: "jacob", age: 25 }, { name: "bill" , age: 25 }]
var newusers = jLinq.from(users).except(additionalusers).select();
>>> newusers = [ { name: "bob" , age: 30 } ]
It's a bit overkill for you at the moment, but it's a robust solution that I was glad to learn about.
目前这对您来说有点矫枉过正,但我很高兴了解它是一个强大的解决方案。
It can do intersects, unions, handle boolean logic and all kinds of great linq style goodness.
它可以做相交、联合、处理布尔逻辑和各种伟大的 linq 风格的优点。
回答by Mogsdad
This is a late answer, but it uses no libraries so some may find it helpful.
这是一个迟到的答案,但它没有使用库,所以有些人可能会觉得它很有帮助。
/**
* Returns a non-destructive Array of elements that are not found in
* any of the parameter arrays.
*
* @param {...Array} var_args Arrays to compare.
*/
Array.prototype.uniqueFrom = function() {
if (!arguments.length)
return [];
var a1 = this.slice(0); // Start with a copy
for (var n=0; n < arguments.length; n++) {
var a2 = arguments[n];
if (!(a2 instanceof Array))
throw new TypeError( 'argument ['+n+'] must be Array' );
for(var i=0; i<a2.length; i++) {
var index = a1.indexOf(a2[i]);
if (index > -1) {
a1.splice(index, 1);
}
}
}
return a1;
}
Example:
例子:
var sheetUsers = ['[email protected]','[email protected]','[email protected]'];
var siteViewers = ['[email protected]','[email protected]','[email protected]'];
var viewersToAdd = sheetUsers.uniqueFrom(siteViewers); // [[email protected]]
var viewersToRemove = siteViewers.uniqueFrom(sheetUsers); // [[email protected]]

