简体   繁体   English

将getIntersect(arr1,arr2)Javascript函数转换为任意数量的参数并在JQuery中

[英]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.

相关问题 我正在尝试编写 function 其中 arr1[0] = arr2[0], arr1[1] = arr2[1], ... 等等。 我该怎么做呢? - I am trying to write a function where arr1[0] = arr2[0], arr1[1] = arr2[1], … and so on. How do I do this? 在给定索引 n 处将 arr1 复制到 arr 2,并且在 function 运行后 arr 1 和 arr2 应该相同 - copy arr1 to arr 2 at given index n and arr 1 and arr2 should be same after function runs arr1.concat(arr2)和[] .concat(arr1,arr2)之间的区别 - Difference Between arr1.concat(arr2) And [].concat(arr1, arr2) 如何在 JavaScript 中将数组 (arr1) 的值推送到新的空数组 (arr2) 中? - How to push a value of an array (arr1) into a new empty array (arr2) in JavaScript? 是否有一个loadash函数可以比较两个数组并仅在arr1中存在来自arr2的所有值时才返回true? - Is there a loadash function to compare two arrays and return true only if all values from arr2 is present in arr1? 制作一个接收两个 arrays 的 function。 将 arr1 和 arr2 中的所有数字相加。 如果 arr1 的总和等于 arr2 返回 true。 如果不是,则为假 - make a function that takes in two arrays. Add all the numbers in arr1 & arr2. If the sum of arr1 is equal to arr2 return true. False if not 如何连接arr1数组和arr2数组(基于arr2数组结构) - How to join arr1 array and arr2 array (based on arr2 array structure) 如何从arr1和arr2获取结果,当ID匹配时我需要从arr1复制内容 - How can I get the result from arr1 and arr2, When the ID matches I need to copy the content from arr1 过滤 arr1 &gt; obj1 &gt; arr2 &gt; obj2 中的多个第一个对象 - filter multiple first objects in arr1 > obj1 > arr2 > obj2 如果arr1中的任何元素等于arr match的obj的ID,则将其保留,并将arr1元素的其余部分放入emptyArr - if any element from arr1 is equal to id of obj of arr match.. leave this and put rest of the arr1 element in emptyArr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM