arrayOperation.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * 判断两数组字符串是否相同(用于按钮权限验证),数组字符串中存在相同时会自动去重(按钮权限标识不会重复)
  3. * @param news 新数据
  4. * @param old 源数据
  5. * @returns 两数组相同返回 `true`,反之则反
  6. */
  7. export function judgementSameArr(newArr: unknown[] | string[], oldArr: string[]): boolean {
  8. const news = removeDuplicate(newArr);
  9. const olds = removeDuplicate(oldArr);
  10. let count = 0;
  11. const leng = news.length;
  12. for (let i in olds) {
  13. for (let j in news) {
  14. if (olds[i] === news[j]) count++;
  15. }
  16. }
  17. return count === leng ? true : false;
  18. }
  19. /**
  20. * 判断两个对象是否相同
  21. * @param a 要比较的对象一
  22. * @param b 要比较的对象二
  23. * @returns 相同返回 true,反之则反
  24. */
  25. export function isObjectValueEqual<T extends Record<string, any>>(a: T, b: T): boolean {
  26. if (!a || !b) return false;
  27. let aProps = Object.getOwnPropertyNames(a);
  28. let bProps = Object.getOwnPropertyNames(b);
  29. if (aProps.length != bProps.length) return false;
  30. for (let i = 0; i < aProps.length; i++) {
  31. let propName = aProps[i];
  32. let propA = a[propName];
  33. let propB = b[propName];
  34. if (!b.hasOwnProperty(propName)) return false;
  35. if (propA instanceof Object) {
  36. if (!isObjectValueEqual(propA, propB)) return false;
  37. } else if (propA !== propB) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. /**
  44. * 原始实现:数组、数组对象去重
  45. * @param arr 数组内容
  46. * @param attr 需要去重的键值(数组对象)
  47. * @returns
  48. */
  49. /*
  50. export function removeDuplicate(arr: EmptyArrayType, attr?: string) {
  51. if (!Object.keys(arr).length) {
  52. return arr;
  53. } else {
  54. if (attr) {
  55. const obj: EmptyObjectType = {};
  56. return arr.reduce((cur: EmptyArrayType[], item: EmptyArrayType) => {
  57. obj[item[attr]] ? '' : (obj[item[attr]] = true && item[attr] && cur.push(item));
  58. return cur;
  59. }, []);
  60. } else {
  61. return [...new Set(arr)];
  62. }
  63. }
  64. }
  65. */
  66. /**
  67. * 优化后实现:数组、数组对象去重
  68. * 支持普通数组和对象数组去重,类型安全,且兼容原有所有调用方式
  69. * @param arr 数组内容
  70. * @param attr 需要去重的键值(数组对象)
  71. * @returns
  72. */
  73. export function removeDuplicate<T>(arr: T[], attr?: string): T[] {
  74. if (!arr.length) {
  75. return arr;
  76. } else {
  77. if (attr) {
  78. const obj: Record<string, boolean> = {};
  79. return arr.reduce((cur: T[], item: T) => {
  80. const key = (item as any)[attr];
  81. if (key && !obj[key]) {
  82. obj[key] = true;
  83. cur.push(item);
  84. }
  85. return cur;
  86. }, []);
  87. } else {
  88. return [...new Set(arr)];
  89. }
  90. }
  91. }
  92. /* 数组、对象深拷贝
  93. * @param value 需要拷贝内容
  94. * @returns
  95. */
  96. export const clone = <T>(value: T): T => {
  97. if (!value) return value;
  98. // 数组
  99. if (Array.isArray(value)) return value.map((item) => clone(item)) as unknown as T;
  100. // 普通对象
  101. if (typeof value === 'object') {
  102. return Object.fromEntries(
  103. Object.entries(value).map(([k, v]: [string, any]) => {
  104. return [k, clone(v)];
  105. })
  106. ) as unknown as T;
  107. }
  108. // 基本类型
  109. return value;
  110. };