JavaScript gets a simple instance of an intersection of multiple arrays

  • 2020-03-27 00:00:43
  • OfStack

You need to get the intersection of multiple arrays in your project, so this example is for a specific scenario. For example, var A = {1000,10001,10002,10003}; Var B = {10002, 10003}; C array var C = {10003}; You need to get an array of the intersection of these three arrays.
          The specific idea is: firstly, the smallest array is A array, and the smallest number of elements is also the length of array a. then, each array is iterated, and the shortest length of these arrays is the minimum length, and the shortest length array is obtained. Then, iterate over the smallest array and each array, and start comparing the equality of the elements, using the counter to determine whether the element exists in each array as an intersection element.
          The idea is relatively simple, but it can achieve the intersection of most groups, the code is as follows:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
 <HEAD> 
  <TITLE> New Document </TITLE> 
  <META NAME="Generator" CONTENT="EditPlus"> 
  <META NAME="Author" CONTENT=""> 
  <META NAME="Keywords" CONTENT=""> 
  <META NAME="Description" CONTENT=""> 
  <script> 
        function getValues(obj){     
            var values = ""; 
            var l = obj.options.length; 
            for (var i=0; i<l; i++) { 
                if (i != (l-1)) { 
                    values += obj.options(i).value + "_"; 
                } 
                else { 
                    values += obj.options(i).value; 
                } 
            } 
            return values; 
        } 

        function _test() { 
            var ids = getValues(document.all.aa); 
            var aa = _getIntersection(ids); 
        } 

        function _getIntersection(src) { 
            var tAry = src.split("_"); 
            //Minimum array
            var minAry = null; 
            var min = tAry[0].split(",").length; //Initializes the first array with the smallest length
            minAry = tAry[0].split(","); 
            for (var i = 1, len = tAry.length; i<len; i++) { 
                var temp = tAry[i].split(","); 
                if (temp.length < min) { 
                    min = temp.length; 
                    minAry = temp; 
                }  
            } 
            alert(" Minimum array: "+minAry); 

            var ret = ''; 
            for (var i = 0, len = minAry.length; i<len; i++) { 
                var srcNum = parseInt(minAry[i]); 
                var counter = 0; 
                for (var j = 0, ll = tAry.length; j<ll; j++) { 
                    var tt = tAry[j].split(","); 
                    for (var k = 0, l = tt.length; k<l; k++) { 
                        var tarNum = parseInt(tt[k]); 
                        if (srcNum == tarNum) { 
                            counter ++; 
                        } 
                    } 
                } 
                if (counter == tAry.length) { 
                    ret += srcNum + ","; 
                } 
            } 
            ret = strSlice(ret, ','); 
            alert(" Intersection is: " + ret); 
        } 

        //Remove the end separator
        function strSlice(str, split){ 
            if ((str!=null && str!="") && (split!=' ')) 
                return ((str.charAt(str.length-1) == split) ? str.substring(0, str.length-1) : str); 
            else 
                return str; 
        } 
  </script> 

 </HEAD> 

 <BODY> 
    <button onclick="javascript:_test();"> test </button> 

    <select name="aa" id="aa" size="6" multiple> 
        <OPTION value="10004,10005,10008,10009,10010,10018"> test 1</OPTION> 
        <OPTION value="10004,10005,10006,10008,10009,10010,10018"> test 2</OPTION> 
        <OPTION value="10004,10005,10006,10008,10009,10010,10018"> test 3</OPTION> 
        <OPTION value="10004,10006,10008"> test 4</OPTION> 
        <OPTION value="10004,10010,10018"> test 5</OPTION> 
    </select> 

 </BODY> 
</HTML> 

Related articles: