arrayOperation.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * 判断子数据数组是否走在总数据里
  3. * @param big 总数据
  4. * @param small 子数据
  5. * @returns 总数据包含子数据 `true`,反之则反
  6. */
  7. export function smallInBig(big: string[], small: string[]): boolean {
  8. return small.every(i => big.includes(i))
  9. }
  10. /**
  11. * 判断两数组是否相同
  12. * @param news 新数据
  13. * @param old 源数据
  14. * @returns 两数组相同返回 `true`,反之则反
  15. */
  16. export function judementSameArr(news: unknown[] | string[], old: string[]): boolean {
  17. let count = 0;
  18. const leng = old.length;
  19. for (let i in old) {
  20. for (let j in news) {
  21. if (old[i] === news[j]) count++;
  22. }
  23. }
  24. return count === leng ? true : false;
  25. }
  26. /**
  27. * 判断两个对象是否相同
  28. * @param a 要比较的对象一
  29. * @param b 要比较的对象二
  30. * @returns 相同返回 true,反之则反
  31. */
  32. export function isObjectValueEqual(a: { [key: string]: any }, b: { [key: string]: any }) {
  33. if (!a || !b) return false;
  34. let aProps = Object.getOwnPropertyNames(a);
  35. let bProps = Object.getOwnPropertyNames(b);
  36. if (aProps.length != bProps.length) return false;
  37. for (let i = 0; i < aProps.length; i++) {
  38. let propName = aProps[i];
  39. let propA = a[propName];
  40. let propB = b[propName];
  41. if (!b.hasOwnProperty(propName)) return false;
  42. if (propA instanceof Object) {
  43. if (!isObjectValueEqual(propA, propB)) return false;
  44. } else if (propA !== propB) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }