js中判斷兩個數(shù)組對象是否完全相等
js判斷兩個數(shù)組對象是否完全相等
如何判斷兩個數(shù)組是否完全相等,如何判斷兩個對象是否完全相等
也是sku庫存配置中用到的
每次往數(shù)組里插入值時,都要判斷之前選中的數(shù)組里面是否已經(jīng)存在了這個數(shù)組對象
arrayEquals(array1, array2) {
// if array1 or array2 is a falsy value, return
if (!array1 || !array2)
return false;
// compare lengths - can save a lot of time
if (array1.length != array2.length)
return false;
for (var i = 0, l = array1.length; i < l; i++) {
// Check if we have nested arrays
if (array1[i] instanceof Array && array2[i] instanceof Array) {
// recurse into the nested arrays
if (!this.arrayEquals(array1[i], array2[i]))
return false;
} else if (array1[i] instanceof Object && array2[i] instanceof Object) {
// 比較含有的對象是否相等
if (!this.objectEquals(array1[i], array2[i]))
return false;
} else if (array1[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
},
objectEquals(object1, object2) {
//For the first loop, we only check for types
for (let propName in object1) {
//Check for inherited methods and properties - like .equals itself
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
//Return false if the return value is different
if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
}
//Check instance type
else if (typeof object1[propName] != typeof object2[propName]) {
//Different types => not equal
return false;
}
}
//Now a deeper check using other objects property names
for (let propName in object2) {
//We must check instances anyway, there may be a property that only exists in object2
//I wonder, if remembering the checked values from the first loop would be faster or not
if (object1.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
} else if (typeof object1[propName] != typeof object2[propName]) {
return false;
}
//If the property is inherited, do not check any more (it must be equa if both objects inherit it)
if (!object1.hasOwnProperty(propName))
continue;
//Now the detail check and recursion
//This returns the script back to the array comparing
/**REQUIRES Array.equals**/
if (object1[propName] instanceof Array && object2[propName] instanceof Array) {
// recurse into the nested arrays
if (!this.arrayEquals(object1[propName], object2[propName]))
return false;
} else if (object1[propName] instanceof Object && object2[propName] instanceof Object) {
// recurse into another objects
if (!this.objectEquals(object1[propName], object2[propName]))
return false;
}
//Normal value comparison for strings and numbers
else if (object1[propName] != object2[propName]) {
return false;
}
}
//If everything passed, let's say YES
return true;
}
js判斷兩個對象是否相等的辦法,包含絕對相等和形狀內(nèi)容相等
在js中對象是引用類型,對象要相等除非是同一個引用,不然就不會相等,如下:
var obj1={0:'a',1:'b',2:'c'}
var obj2={0:'a',1:'b',2:'c'}
console.log(obj1==obj2)
console.log(obj1===obj2)打印都為false,雖然他們模樣一樣,當(dāng)需要判斷對象的形狀和內(nèi)容都一樣的時候,就比如上面的obj1、obj2,怎么辦呢?它來了
完整代碼:
//判斷兩個對象是否相同(包含絕對相等和他們是否有相同的形狀)
function looseEqual (a, b) {
if (a === b) { //如果是絕對相等就直接返回true
return true ;
}
//如果不是絕對相等就哦按的他們是否有相同的形狀
var isObjectA = isObject(a);
var isObjectB = isObject(b);
if (isObjectA && isObjectB) {//兩個均是對象
try {
var isArrayA = Array.isArray(a);
var isArrayB = Array.isArray(b);
if (isArrayA && isArrayB) {//如果都是數(shù)組
if(a.length === b.length){//如果長度相等
return a.every(function (e, i) {//用every和遞歸來比對a數(shù)組和b數(shù)組的每個元素,并返回
return looseEqual(e, b[i])
})
}
//長度都不等直接返回false
return false;
} else if (a instanceof Date && b instanceof Date) {//如果是Date 則直接getTime 比較
return a.getTime() === b.getTime()
} else if (!isArrayA && !isArrayB) {//對象的比較
//拿到兩個對象的key
var keysA = Object.keys(a);
var keysB = Object.keys(b);
if(keysA.length === keysB.length){//如果keys相等
return keysA.every(function (key) {//用every和遞歸來比對a對象和b對象的每個元素值,并返回
return looseEqual(a[key], b[key]);
})
}
//長度都不等直接返回false
return false;
} else {
return false
}
} catch (e) {
return false
}
} else if (!isObjectA && !isObjectB) {//如果都不是對象則按String來處理
return String(a) === String(b)
} else {
return false
}
}
function isObject (obj) {
return obj !== null && typeof obj === 'object'
}測試一波:
//字符
var str1="abc";
var str2="abc";
console.log(looseEqual(str1,str2))
//數(shù)字
var num1=12222;
var num2=12222;
console.log(looseEqual(num1,num2))
//對象
var obj1={0:'a',1:'b',2:'c'}
var obj2={0:'a',1:'b',2:'c'}
console.log(looseEqual(obj1,obj2))
//對象嵌套數(shù)組
var obj1={0:'a',1:'b',2:[1,2,3]}
var obj2={0:'a',1:'b',2:[1,2,3]}
console.log(looseEqual(obj1,obj2))
//類數(shù)組
var a={0:'a',1:'b',2:'c','length':3}
var b={0:'a',1:'b',2:'c','length':3}
console.log(looseEqual(a,b))
//數(shù)組
var list=[1,2,3,4]
var list1=[1,2,3,4]
console.log(looseEqual(list,list1))
//數(shù)組嵌套
list=[1,2,3,[6,7]]
list1=[1,2,3,[6,7]]
console.log(looseEqual(list,list1))
//數(shù)組嵌套對象
list=[1,2,3,{a:'1',b:'7'}]
list1=[1,2,3,{a:'1',b:'7'}]
console.log(looseEqual(list,list1))
var d1 = new Date();
var d2 = new Date();
console.log(looseEqual(d1,d2))
var d3 = new Date();
var d4 ;
//使用延時來賦值d4
setTimeout(function(){
d4 = new Date();
console.log(looseEqual(d3,d4))
},1);
輸出結(jié)果:

除了最后一個時間的,我們用了setTimeout來驗證以外,其他的都是通過的,這個應(yīng)該可以做蠻好的工具函數(shù)了吧,哈哈!
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實現(xiàn)計數(shù)器基礎(chǔ)方法
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)計數(shù)器的基礎(chǔ)方法2017-10-10
,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
layui使用form表單實現(xiàn)post請求頁面跳轉(zhuǎn)的方法
今天小編就為大家分享一篇layui使用form表單實現(xiàn)post請求頁面跳轉(zhuǎn)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
Javascript基于AJAX回調(diào)函數(shù)傳遞參數(shù)實例分析
這篇文章主要介紹了Javascript基于AJAX回調(diào)函數(shù)傳遞參數(shù)的方法,結(jié)合實例形式較為詳細的分析了JavaScript使用ajax傳遞參數(shù)的相關(guān)技巧以及回調(diào)函數(shù)的實現(xiàn)技巧,需要的朋友可以參考下2015-12-12

