实际开发中,总是会遇到数组对象去重的应用,以下示例是我在开发中使用的的删除的方法,仅供参考
<!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>
可以直接复制运行代码,查看打印结果