js数组取并集,第一种写法:
复制代码 代码示例:
<html>
<head>
<script>
function test(){
var x = ['c','d','a','b'];
var y = ['d','c','f'];
for(i=0;i<y.length;i++){
var flag = true;
for(var j=0;j<x.length;j++){
if(x[j]==y[i]){
y.splice(i,1);
flag = false;
break;
}
}
if(flag){
x.push(y[i]);
}
}
alert(x);
}
</script>
</head>
<input type='button' value='点我' onclick='test()'/>
</html>
js数组取并集,第二种写法:
复制代码 代码示例:
<html>
<head>
<script>
function test(){
var x = ['c','d','a','b'];
var y = ['d','c','f'];
var context = {};
for(var i=0;i<x.length;i++){
context[x[i]] = x[i];
}
for(var j=0;j<y.length;j++){
context[y[j]] = y[j];
}
//输出
for(var k in context){
alert(k);
}
}
</script>
</head>
<input type='button' value='点我' onclick='test()'/>
</html>
推荐第二种写法。