JavaScript获取多个数组的交集简单实例
时间:2020-10-10 11:46:18|栏目:JavaScript代码|点击: 次
在项目中需要取得多个数组的交集,故本例只是适用于特定场景。比如A数组 var a = {1000,10001,10002,10003}; B数组 var b = {10002, 10003}; C数组var c = {10003}; 需要取得这三个数组的交集数组。
具体思路为:先初始一个最小数组为A数组,元素个数最小的也是数组A的长度.然后再迭代各个数组,取得这几个数组长度最短的为最小长度,同时得到长度最短数组。然后,迭代最小数组与各个数组,开始比较元素相等,使用计数器来判断是否该元素在各个数组中都存在即为交集元素.
思路比较简单,但是可以实现多数组取得交集,代码如下:
<!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("_");
//最小数组
var minAry = null;
var min = tAry[0].split(",").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("最小数组:"+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("交集是:" + ret);
}
//去掉结尾分隔符
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();">测试</button>
<select name="aa" id="aa" size="6" multiple>
<OPTION value="10004,10005,10008,10009,10010,10018">测试1</OPTION>
<OPTION value="10004,10005,10006,10008,10009,10010,10018">测试2</OPTION>
<OPTION value="10004,10005,10006,10008,10009,10010,10018">测试3</OPTION>
<OPTION value="10004,10006,10008">测试4</OPTION>
<OPTION value="10004,10010,10018">测试5</OPTION>
</select>
</BODY>
</HTML>
具体思路为:先初始一个最小数组为A数组,元素个数最小的也是数组A的长度.然后再迭代各个数组,取得这几个数组长度最短的为最小长度,同时得到长度最短数组。然后,迭代最小数组与各个数组,开始比较元素相等,使用计数器来判断是否该元素在各个数组中都存在即为交集元素.
思路比较简单,但是可以实现多数组取得交集,代码如下:
复制代码 代码如下:
<!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("_");
//最小数组
var minAry = null;
var min = tAry[0].split(",").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("最小数组:"+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("交集是:" + ret);
}
//去掉结尾分隔符
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();">测试</button>
<select name="aa" id="aa" size="6" multiple>
<OPTION value="10004,10005,10008,10009,10010,10018">测试1</OPTION>
<OPTION value="10004,10005,10006,10008,10009,10010,10018">测试2</OPTION>
<OPTION value="10004,10005,10006,10008,10009,10010,10018">测试3</OPTION>
<OPTION value="10004,10006,10008">测试4</OPTION>
<OPTION value="10004,10010,10018">测试5</OPTION>
</select>
</BODY>
</HTML>