lodash difference 过滤相同元素

lodash difference过滤相同元素,排除数组1中的与与其他参数数组相同的元素并返回一个新数组。

difference方法

var result=_.difference(array, arr1,arr2,...)
//array需要被过滤的数组
//用arr1过滤
//用arr2过滤
//result为array过滤的新数组

difference例子

var result=_.difference([3, 2, 1], [4, 2], [1]);
// result 结果为: [3]

完整例子

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>lodash difference例子</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, minimal-ui">
    <script src="https://www.itxst.com/package/lodash/lodash.min.js"></script>
    <script src="https://www.itxst.com/package/jquery-3.5.1/jquery.min.js"></script>
</head>
<body style="padding:10px;">
    <div class="btn" onclick="ck1()">执行difference方法</div>
    <div id="result"></div>
    <script>
      var arr=['google.com','itxst.com','baidu.com'];
      $("#result").html(JSON.stringify(arr));
      function ck1() {
        var arr1=['itxst.com','asp.net'];
        var arr2=['baidu.com'];
        var result=_.difference(arr,arr1,arr2);
        $("#result").html(JSON.stringify(result));
      }
    </script>
     <style>
        .btn {
            border: solid 1px #ddd;
            background-color: #eee;
            border-radius: 5px;
            padding: 5px 12px;
            text-align: center;
            cursor: pointer;
            display: inline-block;
            margin-right: 8px;
        }
       .btn:active {
            border: solid 1px #cac9c9;
            background-color: #cac9c9;
        }
    </style>
</body>
</html>

在线试一试