How do I compare two arrays in jquery and then remove the items from the first array that do not match in both arrays?
如何在jquery中比较两个数组,然后从两个数组中不匹配的第一个数组中删除项目?
I start with this
我开始用这个
var listOne = [1,2,3,4,6,7];
var listTwo = [2,5,6];
I want the result to be like this only listOne is changed.
我希望结果是这样的,只有listOne改变了。
var listOne = [2,6];
var listTwo = [2,5,6];
3 个解决方案
#1
2
For medium sized arrays, the brute force solution should work fine. What you describe seems like it basically just comes down to removing elements from one array that are not present in the other array. So, traverse the first array and remove any element that is not found in the second array:
对于中等大小的数组,蛮力解决方案应该可以正常工作。您所描述的似乎基本上就是从一个数组中删除不存在于另一个数组中的元素。因此,遍历第一个数组并删除第二个数组中没有的元素:
function removeElems(src, permitted) {
// traverse array backwards so iteration not affected when we remove current item
for (var i = src.length - 1; i >= 0; i--) {
// if src element not found in permitted array, remove it from src
if (permitted.indexOf(src[i]) === -1) {
src.splice(i, 1);
}
}
}
var listOne = [1,2,3,4,6,7];
var listTwo = [2,5,6];
removeElems(listOne, listTwo);
Working demo: http://jsfiddle.net/jfriend00/1n1fbpgm/
工作演示:http://jsfiddle.net/jfriend00/1n1fbpgm/
If listTwo
could be very long, I'd probably put all the elements of that array into a temporary object first so checking if an item was in the array was just an object key lookup rather than a linear array search, but for small/medium sized arrays or situations where performance isn't super criticial that extra complexity is likely not warranted.
如果listTwo可能非常长,我可能将这个数组的所有元素放入一个临时对象数组中第一个检查如果一个项目是只是一个对象键查找数组而不是一个线性搜索,但是对于小型/中型数组或性能的情况下并不是超级criticial额外的复杂性可能不是必要的。
#2
2
I would suggest to use .grep and .inArray in jQuery,Like that:
我建议在jQuery中使用.grep和.inArray,如下所示:
var array1 = [1, 2, 3, 4, 6, 7];
var array2 = [2, 5, 6];
var foo = [];
var foo2 = [];
var result = [];
var i = 0;
jQuery.grep(array2, function(el) {
if (jQuery.inArray(el, array1) == -1) {
foo.push(el);
} else {
foo2.push(el);
}
i++;
});
alert(foo2);
alert(foo);
result = $.merge(foo2, foo)
alert(result);
http://jsfiddle.net/csdtesting/u9xES/644/
http://jsfiddle.net/csdtesting/u9xES/644/
#3
0
jQuery is just the wrong library to do so. There's a nice library called lodash
that has a function difference
that does what you're asking about.
jQuery只是一个错误的库。有一个很好的库叫做lodash,它有一个功能上的差异,这就是你要问的。
#1
2
For medium sized arrays, the brute force solution should work fine. What you describe seems like it basically just comes down to removing elements from one array that are not present in the other array. So, traverse the first array and remove any element that is not found in the second array:
对于中等大小的数组,蛮力解决方案应该可以正常工作。您所描述的似乎基本上就是从一个数组中删除不存在于另一个数组中的元素。因此,遍历第一个数组并删除第二个数组中没有的元素:
function removeElems(src, permitted) {
// traverse array backwards so iteration not affected when we remove current item
for (var i = src.length - 1; i >= 0; i--) {
// if src element not found in permitted array, remove it from src
if (permitted.indexOf(src[i]) === -1) {
src.splice(i, 1);
}
}
}
var listOne = [1,2,3,4,6,7];
var listTwo = [2,5,6];
removeElems(listOne, listTwo);
Working demo: http://jsfiddle.net/jfriend00/1n1fbpgm/
工作演示:http://jsfiddle.net/jfriend00/1n1fbpgm/
If listTwo
could be very long, I'd probably put all the elements of that array into a temporary object first so checking if an item was in the array was just an object key lookup rather than a linear array search, but for small/medium sized arrays or situations where performance isn't super criticial that extra complexity is likely not warranted.
如果listTwo可能非常长,我可能将这个数组的所有元素放入一个临时对象数组中第一个检查如果一个项目是只是一个对象键查找数组而不是一个线性搜索,但是对于小型/中型数组或性能的情况下并不是超级criticial额外的复杂性可能不是必要的。
#2
2
I would suggest to use .grep and .inArray in jQuery,Like that:
我建议在jQuery中使用.grep和.inArray,如下所示:
var array1 = [1, 2, 3, 4, 6, 7];
var array2 = [2, 5, 6];
var foo = [];
var foo2 = [];
var result = [];
var i = 0;
jQuery.grep(array2, function(el) {
if (jQuery.inArray(el, array1) == -1) {
foo.push(el);
} else {
foo2.push(el);
}
i++;
});
alert(foo2);
alert(foo);
result = $.merge(foo2, foo)
alert(result);
http://jsfiddle.net/csdtesting/u9xES/644/
http://jsfiddle.net/csdtesting/u9xES/644/
#3
0
jQuery is just the wrong library to do so. There's a nice library called lodash
that has a function difference
that does what you're asking about.
jQuery只是一个错误的库。有一个很好的库叫做lodash,它有一个功能上的差异,这就是你要问的。