JS分隔换行成数组+去重

代码:

var mDPS = "";
        if (multipleDispatchID != '') {
            const arr = multipleDispatchID.split(/[(\r\n)\r\n]+/);
            const arr1 = Unique(arr)
            for (var i = 0; i < arr1.length; i++) {
                if (arr1[i] != '') {
                    mDPS = mDPS + arr1[i] + ',';
                }
            }
        }
    //去重
    function Unique(array) {
        // res用来存储结果
        var res = [];
        for (var i = 0, arrayLen = array.length; i < arrayLen; i++) {
            for (var j = 0, resLen = res.length; j < resLen; j++) {
                if (array[i] === res[j]) {
                    break;
                }
            }
            // 如果array[i]是唯一的,那么执行完循环,j等于resLen
            if (j === resLen) {
                res.push(array[i])
            }
        }
        return res;
    }

 

参阅:https://blog.csdn.net/lxj7607/article/details/105734039/

https://blog.csdn.net/sunyctf/article/details/131132871

 

posted @ 2024-10-28 10:56  蜗牛的礼物  阅读(4)  评论(0编辑  收藏  举报