JavaScript obtains the intersection of multiple arrays.

Source: Internet
Author: User

The intersection of multiple arrays needs to be obtained in the project. Therefore, this example is only applicable to specific scenarios. For example, A array var a = {10002,100, 10003,}; B array var B = {03}; C array var c = }; you need to obtain the intersection array of the three arrays.
First, the minimum array is array A, and the minimum number of elements is the length of array. then iterate the arrays to obtain the minimum length of the arrays and obtain the minimum length array. Then, the minimum iteration array is equal to each array, and the comparison element starts to be equal. The counter is used to determine whether the element exists in each array, that is, the intersection element.
The idea is simple, but multi-array intersection can be achieved. The Code is as follows:
 
Html code
<! 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; // initialize 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:" + ret );
}
 
// Remove the ending 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 = ","> Test 1 </OPTION>
<OPTION value = "10018,"> Test 2 </OPTION>
<OPTION value = "10018,"> Test 3 </OPTION>
<OPTION value = "10004,10006, 10008"> Test 4 </
<OPTION value = "10004,10010, 10018"> Test 5 </OPTION>
</Select>
 
</BODY>
</HTML>

Author: "Loong_Mit"
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.