JS中用 “ === ” 判断两个对象或数组返回都是false,虽然人眼看着是一模一样的,但JS返回就是不相等,无奈自己封装。代码如下:
获取数据的类型
getDataType(data) {
const temp = Object.prototype.toString.call(data)
const types = temp.match(/\b\w+\b/g)
return (types.length === 2) ? types[1] : undefined
}
数组的比较
function AssertionArray(arrOne, arrTwo) {
const arrOneType = getDataType(arrOne)
const arrTwoType = getDataType(arrTwo)
if (arrOneType !== 'Array') {
throw new Error(`Error${arrOne} is not Array`)
}
if (arrTwoType !== 'Array') {
throw new Error(`Error ${arrTwo} is not Array`)
}
arrOne.sort()
arrTwo.sort()
const arrOneLen = arrOne.length
const arrTwoLen = arrTwo.length
if (arrOneLen !== arrTwoLen) {
return false
}
for (let i = 0; i < arrOneLen; i++) {
const arrOneElType = getDataType(arrOne[i])
const arrTwoElType = getDataType(arrTwo[i])
if (arrOneElType !== arrTwoElType)
return false
if (arrOneElType === 'Array') {
if (!AssertionArray(arrOne[i], arrTwo[i])) {
return false
}
continue
} else if (arrOneElType === 'Object') {
if (!AssertionObject(arrOne[i], arrTwo[i])) {
return false
}
continue
} else if (arrOneElType === 'Function') {
continue
}
if (arrOne[i] !== arrTwo[i]) {
return false
}
}
return true
}
对象之间的比较
function AssertionObject(objOne, objTwo) {
const objOneType = getDataType(objOne)
const objTwoType = getDataType(objTwo)
if (objTwoType !== objOneType) {
return false
}
if (objOneType !== 'Object') {
throw new Error(`Error${objOne} is not Object`)
}
if (objTwoType !== 'Object') {
throw new Error(`Error ${objTwo} is not Object`)
}
const objOneKeys = Object.keys(objOne)
const objTwoKeys = Object.keys(objTwo)
const arrOneLen = objOneKeys.length
const arrTwoLen = objTwoKeys.length
if (arrOneLen !== arrTwoLen || !AssertionArray(objOneKeys, objTwoKeys)) {
return false
}
for (let el in objOneKeys) {
const arrOneElValue = objOne[objOneKeys[el]]
const arrTwoElValue = objTwo[objOneKeys[el]]
const arrOneType = getDataType(arrOneElValue)
const arrTwoType = getDataType(arrTwoElValue)
if (arrOneType !== arrTwoType) {
return false
}
if (arrOneType === 'Object') {
if (!AssertionObject(arrOneElValue, arrTwoElValue)) {
return false
}
continue
} else if (arrOneType === 'Array') {
if (!AssertionArray(arrOneElValue, arrTwoElValue)) {
return false
}
continue
} else if (arrOneType === 'Function') {
continue
}
if (arrOneElValue !== arrTwoElValue) {
return false
}
}
return true
}
合在一块
function getDataType(data) {
const temp = Object.prototype.toString.call(data)
const types = temp.match(/\b\w+\b/g)
return (types.length === 2) ? types[1] : undefined
}
function AssertionArray(arrOne, arrTwo) {
const arrOneType = getDataType(arrOne)
const arrTwoType = getDataType(arrTwo)
if (arrOneType !== 'Array') {
throw new Error(`Error${arrOne} is not Array`)
}
if (arrTwoType !== 'Array') {
throw new Error(`Error ${arrTwo} is not Array`)
}
arrOne.sort()
arrTwo.sort()
const arrOneLen = arrOne.length
const arrTwoLen = arrTwo.length
if (arrOneLen !== arrTwoLen) {
return false
}
for (let i = 0; i < arrOneLen; i++) {
const arrOneElType = getDataType(arrOne[i])
const arrTwoElType = getDataType(arrTwo[i])
if (arrOneElType !== arrTwoElType)
return false
if (arrOneElType === 'Array') {
if (!AssertionArray(arrOne[i], arrTwo[i])) {
return false
}
continue
} else if (arrOneElType === 'Object') {
if (!AssertionObject(arrOne[i], arrTwo[i])) {
return false
}
continue
} else if (arrOneElType === 'Function') {
continue
}
if (arrOne[i] !== arrTwo[i]) {
return false
}
}
return true
}
function AssertionObject(objOne, objTwo) {
const objOneType = getDataType(objOne)
const objTwoType = getDataType(objTwo)
if (objTwoType !== objOneType) {
return false
}
if (objOneType !== 'Object') {
throw new Error(`Error${objOne} is not Object`)
}
if (objTwoType !== 'Object') {
throw new Error(`Error ${objTwo} is not Object`)
}
const objOneKeys = Object.keys(objOne)
const objTwoKeys = Object.keys(objTwo)
const arrOneLen = objOneKeys.length
const arrTwoLen = objTwoKeys.length
if (arrOneLen !== arrTwoLen || !AssertionArray(objOneKeys, objTwoKeys)) {
return false
}
for (let el in objOneKeys) {
const arrOneElValue = objOne[objOneKeys[el]]
const arrTwoElValue = objTwo[objOneKeys[el]]
const arrOneType = getDataType(arrOneElValue)
const arrTwoType = getDataType(arrTwoElValue)
if (arrOneType !== arrTwoType) {
return false
}
if (arrOneType === 'Object') {
if (!AssertionObject(arrOneElValue, arrTwoElValue)) {
return false
}
continue
} else if (arrOneType === 'Array') {
if (!AssertionArray(arrOneElValue, arrTwoElValue)) {
return false
}
continue
} else if (arrOneType === 'Function') {
continue
}
if (arrOneElValue !== arrTwoElValue) {
return false
}
}
return true
}
顺便判断一下某数组是否包含某个对象或元素
function AssertionExist(arr, el) {
const arrLen = arr.length
const elType = getDataType(el)
for (let i = 0; i < arrLen; i++) {
const arrElType = getDataType(arr[i])
if (elType !== arrElType) {
continue
}
if (elType === 'Object') {
if (AssertionObject(arr[i], el)) {
return true
}
} else if (elType === 'Array') {
if (AssertionArray(arr[i], el)) {
return true
}
}
}
return false
}
把方法《AssertionExist》改一下可以返回索引
function getIndex(arr, el) {
const arrLen = arr.length
const elType = getDataType(el)
for (let i = 0; i < arrLen; i++) {
const arrElType = getDataType(arr[i])
if (elType !== arrElType) {
continue
}
if (elType === 'Object') {
if (AssertionObject(arr[i], el)) {
return i
}
} else if (elType === 'Array') {
if (AssertionArray(arr[i], el)) {
return i
}
}
}
}
测试一下
1. 测试数组,各种类型嵌套的
一模一样的数据
const arr1 = ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}, ["Banana", "Orange"], {'1': '1'}]
const arr2 = ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}, ["Banana", "Orange"], {'1': '1'}]
结果
console.log(AssertionArray(arr1, arr2)) // true
不一样的数据
const arr1 = ["Banana", "Orange", "Apple", () => {}, {'1': '2'}, {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}, ["Banana", "Orange"], {'1': '1'}]
const arr2 = ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}, ["Banana", "Orange"], {'1': '1'}]
结果
console.log(AssertionArray(arr1, arr2)) //false
2.测试对象(Object)
一模一样的数据,各种类型嵌套
const obj1 = {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}
const obj2 = {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}
结果
console.log(AssertionObject(obj1, obj2)) //true
不一样的数据
const obj1 = {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '2'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}
const obj2 = {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}
结果
console.log(AssertionObject(obj1, obj2)) //false
3. 测试某数组是否包含某对象
数据
const arr1 = ["Banana", "Orange", "Apple", () => {}, {'1': '2'}, {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}, ["Banana", "Orange"], {'1': '1'}]
const obj1 = {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}
结果
console.log(AssertionExist(arr1,obj1)) //true
4. 测试某对象在某数组中位置
数据
const arr1 = ["Banana", "Orange", "Apple", () => {}, {'1': '2'}, {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}, ["Banana", "Orange"], {'1': '1'}]
const obj1 = {'2': ["Banana", "Orange", "Apple", () => {}, {'1': '1'}, {'2': '2'}, ["Banana", "Orange"], {'1': '1'}]}
结果
console.log(getIndex(arr1,obj1)) // 5