JavaScript gets the intersection of multiple arrays simple example _javascript tips

Source: Internet
Author: User
The intersection of multiple arrays needs to be achieved in the project, so this example applies only to specific scenarios. For example, a array of var a = {1000,10001,10002,10003}; b array var b = {10002, 10003}; C array var c = {10003}; You need to get an array of intersections of these three arrays.
The idea is as follows: first, a minimum array is an array of a, and the smallest number of elements is also the length of the array A. Then iterate over each array, get the shortest length of these array lengths, and get the shortest length array. The iteration's smallest array is then compared to the array, and the element is equal, using a counter to determine whether the element exists in each array as an intersection element.
The idea is relatively simple, but you can achieve the intersection of most groups, the code is as follows:

Copy Code code 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 ("_");
Smallest array
var minary = null;
var min = tary[0].split (","). Length; Initializes the first array of 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 ("Smallest 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 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 ();" > Testing </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 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.