<html>
<head>
<script type="text/javascript">
var a=['a','b','c','d','e'];
var b=['a','b','f','g','h'];
var arr1 = intersection(a,b);
alert(arr1);
var arr2 = chaji(a,b);
alert(arr2);
var arr3 = inANotInB(a,b);
alert(arr3);
//a,b中重复的部分
function intersection(a,b){
var obj = new Object();
for(var i =0,len = a.length;i<len;i++){
obj[a[i]] = a[i];
}
for(var i =0,len = b.length;i<len;i++){
obj[b[i]] = b[i];
}
var arr = new Array();
var i = 0;
for(var per in obj){
arr[i++] = obj[per];
}
return arr;
}
//a与b不重复的部分
function chaji(a,b){
var obj = new Object();
for(var i =0,len = a.length;i<len;i++){
obj[a[i]] = 1;
}
for(var i =0,len = b.length;i<len;i++){
obj[b[i]] = obj[b[i]]?2:1;
}
var arr = new Array();
var i = 0;
for(var per in obj){
if(obj[per] == 1){
arr[i++] = per;
}
}
return arr;
}
//b与a不重复的部分
function inANotInB(a,b){
var obj = new Object();
for(var i =0,len = a.length;i<len;i++){
obj[a[i]] = 1;
}
for(var i =0,len = b.length;i<len;i++){
if(obj.hasOwnProperty(b[i])){
obj[b[i]] = undefined;
}
}
var arr = new Array();
var i = 0;
for(var per in obj){
if(obj[per]){
arr[i++] = per;
}
}
return arr;
}
</script>
</head>
<body>
</body>
</html>
本文链接:https://my.lmcjl.com/post/10887.html
4 评论