diff --git a/src/utils/arrayOperation.ts b/src/utils/arrayOperation.ts index c4b1aab9a3debf9d3b7606d183e8061af3724280..2e7188c8357274525cd36aa2662aac7429de644b 100644 --- a/src/utils/arrayOperation.ts +++ b/src/utils/arrayOperation.ts @@ -1,17 +1,3 @@ -// Judge whether two array strings are the same -export function judementSameArr(newArr: unknown[] | string[], oldArr: string[]): boolean { - const news = removeDuplicate(newArr) - const olds = removeDuplicate(oldArr) - let count = 0 - const leng = olds.length - for (const i in olds) { - for (const j in news) { - if (olds[i] === news[j]) count++ - } - } - return count === leng -} - // Judge whether two array strings are the same export function judementSameArr(newArr: unknown[] | string[], oldArr: string[]): boolean { const news = removeDuplicate(newArr); @@ -26,21 +12,40 @@ export function judementSameArr(newArr: unknown[] | string[], oldArr: string[]): return count === leng; } +// Judge whether two objects are the same +export function isObjectValueEqual(a: { [key: string]: any }, b: { [key: string]: any }) { + 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 (!Object.prototype.hasOwnProperty.call(b, propName)) return false; + if (propA instanceof Object) { + if (!isObjectValueEqual(propA, propB)) return false; + } else if (propA !== propB) { + return false; + } + } + return true; +} + // Array and array object de duplication export function removeDuplicate(arr: any, attr?: string) { if (!arr && !arr.length) { - return arr + return arr; } else { - if (attr) { - const obj: any = {} - const newArr = arr.reduce((cur: any, item: any) => { - obj[item[attr]] ? '' : (obj[item[attr]] = true && item[attr] && cur.push(item)) - return cur - }, []) - return newArr - } else { - return Array.from(new Set([...arr])) - } + if (attr) { + const obj: any = {}; + const newArr = arr.reduce((cur: any, item: any) => { + obj[item[attr]] ? '' : (obj[item[attr]] = true && item[attr] && cur.push(item)); + return cur; + }, []); + return newArr; + } else { + return Array.from(new Set([...arr])); + } } - } - \ No newline at end of file +}