JS 差集

 

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh" dir="ltr">   
    <script type="text/javascript">   
    var arr1 = [1,3,4,5,6,7,8];   
    var arr2 = [1,2,3,4];   
    var arr3 = new Array();   
    function test(){   
        for(var i=0; i < arr1.length; i++){   
            var flag = true;   
            for(var j=0; j < arr2.length; j++){   
                if(arr1[i] == arr2[j])   
                flag = false;   
            }   
            if(flag)   
            arr3.push(arr1[i]);   
        }   
    alert("差集:"+arr3.toString());   
    }   
    </script>   
    <body>   
    <a href="" onclick="test()">test</a>   
    </body>   
    </html>  

 

 

你可能感兴趣的:(js)