Skip to content

Instantly share code, notes, and snippets.

@muzi131313
Last active May 13, 2021 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muzi131313/255494fee2c6adacf138d04c3c693c7b to your computer and use it in GitHub Desktop.
Save muzi131313/255494fee2c6adacf138d04c3c693c7b to your computer and use it in GitHub Desktop.
compare two array, eg: [1, {a: 123}]
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