1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function deduplicatObj(arr: Array<any>) {
const cloneArr = JSON.parse(JSON.stringify(arr));
const res = cloneArr.map((item: any) => {
const types = Object.prototype.toString.call(item);
if (types === '[object Array]') {
return handleArr(item);
} else if (types === '[object Object]') {
return handleObj(item);
} else {
return item;
}
});
return res;
}
function handleArr(data: Array<any>) {
return JSON.parse(JSON.stringify(deduplicatObj(data))).sort();
}
function handleObj(data: any) {
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
const element = data[key];
const types = Object.prototype.toString.call(element);
if (types === '[object Array]') {
data[key] = handleArr(element);
} else if (types === '[object Object]') {
data[key] = handleObj(element);
} else {
//
}
}
}
// 把对象按照默认顺序排序
const keyslist = Object.keys(data).sort();
const myobj = {} as any;
keyslist.map((item: string) => {
myobj[item] = data[item];
});
return myobj;
}
/**
* 数组对象去重 返回一个新数组
* @param data - 原始数组
**/
export const noRepeatObjInArr = function (data: Array<any>) {
const res = deduplicatObj(data);
const mySet: Set<string> = new Set();
res.map((item: any) => {
const JSONstr = JSON.stringify(item);
mySet.add('JSON_' + JSONstr);
});
const arr = [] as any[];
for (const item of mySet) {
const itemArr = item.split('JSON_', 2);
arr.push(JSON.parse(itemArr[1]));
}
return arr;
};