js数组对象去重(同时判断对象中的每一个属性,若其对应的属性值都相同,则去重)

 1 let arr = [
 2       {
 3         maxDeptCode: "md3",
 4         maxDeptName: "泡泡",
 5         minDeptCode: "md301",
 6         minDeptName: "泡泡少儿",
 7         schoolId: 1,
 8         schoolName: "北京",
 9       },
10       {
11         maxDeptCode: "md2",
12         maxDeptName: "中学",
13         minDeptCode: "md201",
14         minDeptName: "中学一对一",
15         schoolId: 1,
16         schoolName: "北京",
17       }, {
18         maxDeptCode: "md3",
19         maxDeptName: "泡泡",
20         minDeptCode: "md301",
21         minDeptName: "泡泡少儿",
22         schoolId: 1,
23         schoolName: "北京",
24       },
25     ];
26 
27 
28 
29     function process(arr) {
30       // 缓存用于记录
31       const cache = [];
32       for (const t of arr) {
33         // 检查缓存中是否已经存在
34         if (cache.find(c => c.maxDeptCode === t.maxDeptCode && c.minDeptCode === t.minDeptCode)) {
35           // 已经存在说明以前记录过,现在这个就是多余的,直接忽略
36           continue;
37         }
38         // 不存在就说明以前没遇到过,把它记录下来
39         cache.push(t);
40       }
41 
42       // 记录结果就是过滤后的结果
43       return cache;
44     } 
45 
46     console.log(process(arr));

 

打印结果如下:

 

posted @ 2020-03-02 17:47  最初的样子  阅读(3500)  评论(0编辑  收藏  举报