JavaScript 数组对象去重

实际开发中,总是会遇到数组对象去重的应用,以下示例是我在开发中使用的的删除的方法,仅供参考

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组去重</title>
</head>

<body>
    <script>
        const originalArray = [
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "银行总行",
                "LEVELUNIT": "1",
                "APP_ID": "62",
                "SUPERUNITID": "0000",
                "UNITSEQ": "500",
                "ID": "500",
                "UNITNAME": "银行总行",
                "UNITID": "500",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "银行总行",
                "LEVELUNIT": "1",
                "APP_ID": "62",
                "SUPERUNITID": "0000",
                "UNITSEQ": "500",
                "ID": "500",
                "UNITNAME": "银行总行",
                "UNITID": "500",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "北区分行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,002",
                "ID": "002",
                "UNITNAME": "北区分行",
                "UNITID": "002",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "东区分行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,003",
                "ID": "003",
                "UNITNAME": "东区分行",
                "UNITID": "003",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "南区分行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,004",
                "ID": "004",
                "UNITNAME": "南区分行",
                "UNITID": "004",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "南区分行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,004",
                "ID": "004",
                "UNITNAME": "南区分行",
                "UNITID": "004",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "北京分行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,005",
                "ID": "005",
                "UNITNAME": "北京分行",
                "UNITID": "005",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "天津分行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,006",
                "ID": "006",
                "UNITNAME": "天津分行",
                "UNITID": "006",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "海南银行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,100001",
                "ID": "100001",
                "UNITNAME": "海南银行",
                "UNITID": "100001",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "海南银行",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,100001",
                "ID": "100001",
                "UNITNAME": "海南银行",
                "UNITID": "100001",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "北京分公司",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,800",
                "ID": "800",
                "UNITNAME": "北京分公司",
                "UNITID": "800",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "上海分公司",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,900",
                "ID": "900",
                "UNITNAME": "上海分公司",
                "UNITID": "900",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "上海分公司",
                "LEVELUNIT": "2",
                "APP_ID": "62",
                "SUPERUNITID": "500",
                "UNITSEQ": "500,900",
                "ID": "900",
                "UNITNAME": "上海分公司",
                "UNITID": "900",
                "ISLEAF": false
            },
            {
                "INSTU_CDE": "0000",
                "ORG_NAME": "三亚分行",
                "LEVELUNIT": "3",
                "APP_ID": "62",
                "SUPERUNITID": "100001",
                "UNITSEQ": "500,100001,100002",
                "ID": "100002",
                "UNITNAME": "三亚分行",
                "UNITID": "100002",
                "ISLEAF": true
            }
        ]
        console.log('原始数组', originalArray.length, originalArray)
        // 输出为 -----------> 原始数组 14 [{"INSTU_CDE":"0000","ORG_NAME":"银行总行","LEVELUNIT":"1","APP_ID":"62","SUPERUNITID":"0000","UNITSEQ":"500","ID":"500","UNITNAME":"银行总行","UNITID":"500","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"银行总行","LEVELUNIT":"1","APP_ID":"62","SUPERUNITID":"0000","UNITSEQ":"500","ID":"500","UNITNAME":"银行总行","UNITID":"500","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北区分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,002","ID":"002","UNITNAME":"北区分行","UNITID":"002","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"东区分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,003","ID":"003","UNITNAME":"东区分行","UNITID":"003","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"南区分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,004","ID":"004","UNITNAME":"南区分行","UNITID":"004","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"南区分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,004","ID":"004","UNITNAME":"南区分行","UNITID":"004","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北京分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,005","ID":"005","UNITNAME":"北京分行","UNITID":"005","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"天津分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,006","ID":"006","UNITNAME":"天津分行","UNITID":"006","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"海南银行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,100001","ID":"100001","UNITNAME":"海南银行","UNITID":"100001","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"海南银行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,100001","ID":"100001","UNITNAME":"海南银行","UNITID":"100001","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北京分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,800","ID":"800","UNITNAME":"北京分公司","UNITID":"800","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"上海分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,900","ID":"900","UNITNAME":"上海分公司","UNITID":"900","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"上海分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,900","ID":"900","UNITNAME":"上海分公司","UNITID":"900","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"三亚分行","LEVELUNIT":"3","APP_ID":"62","SUPERUNITID":"100001","UNITSEQ":"500,100001,100002","ID":"100002","UNITNAME":"三亚分行","UNITID":"100002","ISLEAF":true}]
        let resultArray = [];
        const arrayObj = {};
        resultArray = originalArray.reduce((item, next) => {
            arrayObj[next['ID']] ? '' : arrayObj[next['ID']] = true && item.push(next);
            return item;
        }, [])
        console.log('去重后的数组', resultArray.length, resultArray)
        // 输出为 -----------> 去重后的数组 10 [{"INSTU_CDE":"0000","ORG_NAME":"银行总行","LEVELUNIT":"1","APP_ID":"62","SUPERUNITID":"0000","UNITSEQ":"500","ID":"500","UNITNAME":"银行总行","UNITID":"500","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北区分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,002","ID":"002","UNITNAME":"北区分行","UNITID":"002","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"东区分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,003","ID":"003","UNITNAME":"东区分行","UNITID":"003","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"南区分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,004","ID":"004","UNITNAME":"南区分行","UNITID":"004","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北京分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,005","ID":"005","UNITNAME":"北京分行","UNITID":"005","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"天津分行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,006","ID":"006","UNITNAME":"天津分行","UNITID":"006","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"海南银行","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,100001","ID":"100001","UNITNAME":"海南银行","UNITID":"100001","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"北京分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,800","ID":"800","UNITNAME":"北京分公司","UNITID":"800","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"上海分公司","LEVELUNIT":"2","APP_ID":"62","SUPERUNITID":"500","UNITSEQ":"500,900","ID":"900","UNITNAME":"上海分公司","UNITID":"900","ISLEAF":false},{"INSTU_CDE":"0000","ORG_NAME":"三亚分行","LEVELUNIT":"3","APP_ID":"62","SUPERUNITID":"100001","UNITSEQ":"500,100001,100002","ID":"100002","UNITNAME":"三亚分行","UNITID":"100002","ISLEAF":true}]

    </script>
</body>

</html>

可以直接复制运行代码,查看打印结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值