DigitalFactory/Web/src/utils/arrayOperation.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-03-18 01:36:19 +00:00
/**
*
* @param news
* @param old
* @returns `true`
*/
export function judgementSameArr(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)];
}
}
}