wujingjing
2025-01-14 12a89593d13fa38810c7af54c7ea8cb72ae65a10
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
65
/**
 * 判断两数组字符串是否相同(用于按钮权限验证),数组字符串中存在相同时会自动去重(按钮权限标识不会重复)
 * @param news 新数据
 * @param old 源数据
 * @returns 两数组相同返回 `true`,反之则反
 */
export function judementSameArr(newArr: unknown[] | string[], oldArr: string[]): boolean {
    const news = removeDuplicate(newArr);
    const olds = removeDuplicate(oldArr);
    let count = 0;
    const leng = news.length;
    for (let i in olds) {
        for (let j in news) {
            if (olds[i] === news[j]) count++;
        }
    }
    return count === leng ? true : false;
}
 
/**
 * 判断两个对象是否相同
 * @param a 要比较的对象一
 * @param b 要比较的对象二
 * @returns 相同返回 true,反之则反
 */
export function isObjectValueEqual<T>(a: T, b: T): boolean {
    if (!a || !b) return false;
    let aProps = Object.getOwnPropertyNames(a);
    let bProps = Object.getOwnPropertyNames(b);
    if (aProps.length != bProps.length) return false;
    for (let i = 0; i < aProps.length; i++) {
        let propName = aProps[i];
        let propA = a[propName];
        let propB = b[propName];
        if (!b.hasOwnProperty(propName)) return false;
        if (propA instanceof Object) {
            if (!isObjectValueEqual(propA, propB)) return false;
        } else if (propA !== propB) {
            return false;
        }
    }
    return true;
}
 
/**
 * 数组、数组对象去重
 * @param arr 数组内容
 * @param attr 需要去重的键值(数组对象)
 * @returns
 */
export function removeDuplicate(arr: EmptyArrayType, attr?: string) {
    if (!Object.keys(arr).length) {
        return arr;
    } else {
        if (attr) {
            const obj: EmptyObjectType = {};
            return arr.reduce((cur: EmptyArrayType[], item: EmptyArrayType) => {
                obj[item[attr]] ? '' : (obj[item[attr]] = true && item[attr] && cur.push(item));
                return cur;
            }, []);
        } else {
            return [...new Set(arr)];
        }
    }
}