javascript 在 jQuery 中合并两个数组、存储唯一元素和排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22208966/
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
Merging of two arrays, store unique elements, and sorting in jQuery
提问by Monibrata
var Arr1 = [1,3,4,5,6];
var Arr2 = [4,5,6,8,9,10];
I am trying to do merge these two arrays and output coming is [1,3,4,5,6,4,5,6]
我正在尝试合并这两个数组,输出是 [1,3,4,5,6,4,5,6]
I have used $.merge(Arr1, Arr2);
this piece to merge them. Using alert I can see the merged array like above.
我用$.merge(Arr1, Arr2);
这件作品来合并它们。使用警报我可以看到如上所示的合并数组。
Now my question is how can I get the following output:
[1,3,4,5,6,8,9,10]
现在我的问题是如何获得以下输出:
[1,3,4,5,6,8,9,10]
i.e. the elements should be unique as well as sorted in the same manner I have mentioned.
即元素应该是唯一的,并以我提到的相同方式排序。
Please help.
请帮忙。
回答by Nope
You can use Array.prototype.sort()to do a real numeric sort and use Array.prototype.filter()to only return the unique elements.
您可以使用Array.prototype.sort()进行真正的数字排序,并使用Array.prototype.filter()仅返回唯一元素。
You can wrap it into a helper similar to this:
你可以把它包装成一个类似于这样的助手:
var concatArraysUniqueWithSort = function (thisArray, otherArray) {
var newArray = thisArray.concat(otherArray).sort(function (a, b) {
return a > b ? 1 : a < b ? -1 : 0;
});
return newArray.filter(function (item, index) {
return newArray.indexOf(item) === index;
});
};
Note that the custom sort function works with numeric elements only, so if you want to use it for strings or mix strings with numbers you have to update it off course to take those scenarios into account, though the rest should not change much.
请注意,自定义排序函数仅适用于数字元素,因此,如果您想将其用于字符串或将字符串与数字混合使用,则必须将其更新为将这些情况考虑在内,但其余部分不应有太大变化。
Use it like this:
像这样使用它:
var arr1 = [1, 3, 4, 5, 6];
var arr2 = [4, 5, 6, 8, 9, 10];
var arrAll = concatArraysUniqueWithSort(arr1, arr2);
arrAll
will now be [1, 3, 4, 5, 6, 8, 9, 10]
arrAll
现在将是 [1, 3, 4, 5, 6, 8, 9, 10]
DEMO- concatenate 2 arrays, sort and remove duplicates
演示- 连接 2 个数组,排序并删除重复项
There is many ways of doing this I'm sure. This was just the most concise I could think off.
我敢肯定,有很多方法可以做到这一点。这只是我能想到的最简洁的。
回答by Milan MiNo Michalec
merge two or more arrays + remove duplicities + sort()
合并两个或多个数组 + 删除重复项 + sort()
jQuery.unique([].concat.apply([],[[1,2,3,4],[1,2,3,4,5,6],[3,4,5,6,7,8]])).sort();
回答by Giorgos Pippos
I did that as follows, where t1 and t2 are my two tables.
我这样做如下,其中 t1 和 t2 是我的两个表。
The first command put the values of the table t2 to the t1. The second command removes the duplicate values from the table.
第一个命令将表 t2 的值放入 t1。第二个命令从表中删除重复值。
$.merge(t1, t2);
$.unique(t1);
回答by Paul S.
This looks like a job for Array.prototype.indexOf
这看起来像一份工作 Array.prototype.indexOf
var arr3 = arr1.slice(), // clone arr1 so no side-effects
i; // var i so it 's not global
for (i = 0; i < arr2.length; ++i) // loop over arr2
if (arr1.indexOf(arr2[i]) === -1) // see if item from arr2 is in arr1 or not
arr3.push(arr2[i]); // it's not, add it to arr3
arr3.sort(function (a, b) {return a - b;});
arr3; // [1, 3, 4, 5, 6, 8, 9, 10]
回答by Jay
Using underscore.js:
使用 underscore.js:
_.union([1, 2, 3], [101, 2, 1, 10], [2, 1]).sort(function(a,b){return a-b});
=> [1, 2, 3, 10, 101]
This example is taken directly from underscore.js, a popular JS library which complements jQuery
这个例子直接取自underscore.js,一个流行的 JS 库,它补充了 jQuery
回答by avoid3d
a = [1, 2, 3]
b = [2, 3, 4]
$.unique($.merge(a, b)).sort(function(a,b){return a-b}); -> [1, 2, 3, 4]
Update: This is a bad idea, since the 'unique' function is not meant for use on numbers or strings. However, if you must then the sort function needs to be told to use a new comparator since by default it sorts lexicographically.
更新:这是一个坏主意,因为 'unique' 函数不适用于数字或字符串。但是,如果必须,则需要告诉 sort 函数使用新的比较器,因为默认情况下它按字典顺序排序。