比较与jQuery阵列 [英] Compare arrays with jQuery
问题描述
可能显示的文件:结果
<一href=\"http://stackoverflow.com/questions/1885557/simplest-$c$c-for-array-intersection-in-javascript\">Simplest code代表在JavaScript 结果阵列路口
如何在Javascript 合并两个数组
Possible Duplicates:
Simplest code for array intersection in javascript
How to merge two arrays in Javascript
有三个数组:
var items = Array(523,3452,334,31,5346);
var items_used = Array(3452,31,4123);
var items_new = Array();
第一个是一般情况下,第二个是currenly中使用的项目。第三个包括从所述第一阵列中的所有项目,巫未在第二输入
First one is general, second is the items currenly in use. Third one includes all the items from the first array, witch are not mentioned in second.
我如何从第一个数组项删除,巫婆在第二使用,并将结果写入第三个数组?
How do I remove from the first array items, witch are used in second, and write the result to the third array?
我们应该得到 items_new =阵列(523,334,5346)
。 3452
和 31
被删除,因为它们在第二个数组中提到。
We should get items_new = Array(523, 334, 5346)
. 3452
and 31
are removed, because they are mentioned in second array.
推荐答案
您可以这样做:
var items = Array(523,3452,334,31,5346);
var items_used = Array(3452,31,4123);
var items_compared = Array();
$.each(items, function(i, val){
if($.inArray(val, items_used) < 0)
items_compared.push(val);
});
这就是它
这篇关于比较与jQuery阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!