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

时间:2012-01-27 20:07:24

标签: javascript jquery arrays json function

这是一个两部分问题。第一部分是将下面的函数转换为接受任意数量的数组。

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;
}

此功能片段归功于此帖中的Ian和Jeffrey。 http://www.falsepositives.com/index.php/2009/12/01/javascript-function-to-get-the-intersect-of-2-arrays/

我感兴趣,因为使用哈希表的性能比Underscore Utility带和jquery-rich-array插件的索引方法好得多。我知道Jquery有一个方法,jQuery.inArray(),但是文档听起来好像它也使用索引,我正在寻找最佳的性能解决方案来排序超过一万个元素的数组。

我的问题的第二部分是让Jquery友好。假设下面的JSON对象,使用Jquery如何1)只选择arrays1-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"]
    }
}

以上答案是数组= [“星期日”,“星期五”]

1 个答案:

答案 0 :(得分:1)

我们可以首先使用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));

对于jQuery,如果从AJAX回调加载JSON,那么:

$.ajax({
    ...
    dataType: 'json',
    success: function(response){
        var container = response.Container1;
        var intersection = getIntersectN(container.Array1, ... container.Array5);
    }
});