[英]Convert getIntersect(arr1, arr2) Javascript function to any number of arguments and in JQuery
This is a two part question. 这是一个两部分的问题。 The first part is converting the function below to accept any number of arrays. 第一部分是转换下面的函数以接受任意数量的数组。
function getIntersect(arr1, arr2) {
var r = [], o = {}, l = arr2.length, i, v;
for (i = 0; i < l; i++) {
o[arr2[i]] = true;
}
l = arr1.length;
for (i = 0; i < l; i++) {
v = arr1[i];
if (v in o) {
r.push(v);
}
}
return r;
}
This function snippet is thanks to Ian and Jeffrey from this post. 这个功能片段要感谢这篇文章中的Ian和Jeffrey。 http://www.falsepositives.com/index.php/2009/12/01/javascript-function-to-get-the-intersect-of-2-arrays/ http://www.falsepositives.com/index.php/2009/12/01/javascript-function-to-get-the-intersect-of-2-arrays/
I am interested because the performance of using the hash table is so much better than the index method of the Underscore Utility belt and the jquery-rich-array plugin. 我很感兴趣,因为使用哈希表的性能比Underscore Utility带和jquery-rich-array插件的索引方法好得多。 I am aware that Jquery has a method, jQuery.inArray(), however the docs make it sound like it too uses index, and I am looking for the optimal performance solution to sort through arrays with over ten thousand elements. 我知道Jquery有一个方法jQuery.inArray(),但是文档听起来好像它也使用索引,并且我正在寻找最佳性能的解决方案以对具有一万个元素的数组进行排序。
The second part of my question is making it Jquery friendly. 我的问题的第二部分是使其对Jquery友好。 Presuming the JSON object below, using Jquery how to 1) select only the arrays1-5, load them into the function and return one array. 假定下面的JSON对象,使用Jquery如何1)仅选择数组1-5,将它们加载到函数中并返回一个数组。
{
"Container1": {
"Array1": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
"Array2": ["Sunday", "Thursday", "Friday", "Saturday"],
"Array3": ["Sunday", "Friday", "Saturday"],
"Array4": ["Sunday", "Friday", "Garbage"],
"Array5": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
"ArrayOthers1": ["1", "6", "8", "5"],
"ArrayOthers2": ["1", "6", "8", "5"],
"ArrayOthers3": ["1", "6", "8", "5"]
}
}
The answer to the above is an array = ["Sunday","Friday"] 上面的答案是一个数组= [“ Sunday”,“ Friday”]
We can start by using your getIntersect()
function as a helper to a newly created getIntersectN()
function: 我们可以从使用getIntersect()
函数作为新创建的getIntersectN()
函数的助手开始:
function getIntersect(arr1, arr2) {
var r = [], o = {}, l = arr2.length, i, v;
for (i = 0; i < l; i++) {
o[arr2[i]] = true;
}
l = arr1.length;
for (i = 0; i < l; i++) {
v = arr1[i];
if (v in o) {
r.push(v);
}
}
return r;
}
function getIntersectN(){
// call signature: getIntersectN(arr1, arr2, ..., arrN)
// NOTE: alternatively you can explicitly pass in an 'args' array to change the call signature to:
// getIntersectN(args) [then just replace 'arguments' below with 'args']
if(arguments.length == 0) return [];
else if(arguments.length == 1) return arguments[0];
var intersect = arguments[0];
for (var i = 1; i < arguments.length; i++){
intersect = getIntersect(intersect, arguments[i]);
}
return intersect;
}
var data = {
"Array1": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
"Array2": ["Sunday", "Thursday", "Friday", "Saturday"],
"Array3": ["Sunday", "Friday", "Saturday"],
"Array4": ["Sunday", "Friday", "Garbage"],
"Array5": ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
"ArrayOthers1": ["1", "6", "8", "5"],
"ArrayOthers2": ["1", "6", "8", "5"],
"ArrayOthers3": ["1", "6", "8", "5"]
};
console.log(getIntersectN(data.Array1, data.Array2, data.Array3, data.Array4, data.Array5));
As for the jQuery, if you are loading the JSON from an AJAX callback, then: 至于jQuery,如果要从AJAX回调加载JSON,则:
$.ajax({
...
dataType: 'json',
success: function(response){
var container = response.Container1;
var intersection = getIntersectN(container.Array1, ... container.Array5);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.