Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:51:37+00:00 2026-05-28T14:51:37+00:00

This is a two part question. The first part is converting the function below

  • 0

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.
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. 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.

The second part of my question is making it Jquery friendly. Presuming the JSON object below, using Jquery how to 1) select only the arrays1-5, load them into the function and return one array.

{
    "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”]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:51:38+00:00Added an answer on May 28, 2026 at 2:51 pm

    We can start by using your getIntersect() function as a helper to a newly created getIntersectN() 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;
    }
    
    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:

    $.ajax({
        ...
        dataType: 'json',
        success: function(response){
            var container = response.Container1;
            var intersection = getIntersectN(container.Array1, ... container.Array5);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a two-part question: First, I am interested to know what the best
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is kinda....a two part question. The first one is much more important than
This is a two part question - first I need to get every element
This is a two part question: Part 1 First, dealing with calculating the entropy
This is a two part question. The first is a specific question about DataSets
This is a two part question from a WPF animation newbie. First, here is
This is really a two part question. First, is it possible to detect if
This is a two part question, but first some background information: I have a
This is a two part question. The first question is. I am looking for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.