Last active
May 13, 2021 06:57
-
-
Save muzi131313/255494fee2c6adacf138d04c3c693c7b to your computer and use it in GitHub Desktop.
compare two array, eg: [1, {a: 123}]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function copyArray(value) { | |
return JSON.parse(JSON.stringify(value)) | |
} | |
/** | |
* @name isArrayEqual | |
* @param {Array} arrayA 数组A | |
* @param {Array} arrayB 数组B | |
* @description 比较两个数组,内容可以顺序不一致 | |
* @returns | |
*/ | |
function isArrayEqual(arrayA = [], arrayB = []) { | |
let arrayBCopy = copyArray(arrayB); | |
if (arrayA.length === arrayBCopy.length) { | |
let notSame = false | |
arrayA.some((arrayAItem) => { | |
const arrayBIndex = arrayBCopy.indexOf(arrayAItem) | |
if (arrayBIndex !== -1) { | |
arrayBCopy.splice(arrayBIndex, 1) | |
} | |
else { | |
notSame = true | |
return true | |
} | |
}) | |
return !notSame | |
} | |
return false | |
} | |
console.log('_compareA: ', isArrayEqual([1, 3], [1, 3])) // true | |
console.log('_compareB: ', isArrayEqual([1, 3], [1, 4])) // false | |
console.log('_compareB: ', isArrayEqual([1, {a: 123}], [1, {a: 123}])) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment