js比較兩個(gè)單獨(dú)的數(shù)組或?qū)ο笫欠裣嗟鹊膶?shí)例代碼
所謂js的中的傳值,其實(shí)也就是說(shuō)5種基本數(shù)據(jù)類型(null,undefind,boolean,number,string)
傳引用也就是說(shuō)的那個(gè)引用數(shù)據(jù)類型,(array和object)
基本數(shù)據(jù)類型的值不可變,而引用數(shù)據(jù)類型的值是可變的
所以當(dāng)你比較數(shù)組和對(duì)象時(shí),都是false;除非你是克隆的原份數(shù)據(jù)
即: var a = { name: "李四" }; var b = a;
大家通常稱對(duì)象為引用類型,以此來(lái)和基本類型進(jìn)行區(qū)分; 而對(duì)象值都是引用,所以的對(duì)象的比較也叫引用的比較,當(dāng)且當(dāng)他們都指向同一個(gè)引用時(shí),即都引用的同一個(gè)基對(duì)象時(shí),它們才相等.
1.比較兩個(gè)單獨(dú)的數(shù)組是否相等
JSON.stringify(a1) == JSON.stringify(a2)
或
a1.toString() == a2.toString()
要判斷2個(gè)數(shù)組是否相同,把數(shù)組轉(zhuǎn)換成字符串進(jìn)行比較。
如果要比較兩個(gè)數(shù)組的元素是否相等,則:
JSON.stringify([1,2,3].sort()) === JSON.stringify([3,2,1].sort());
或
[1,2,3].sort().toString() === [3,2,1].sort().toString();
判斷2個(gè)數(shù)組是否相同,首先要把數(shù)組進(jìn)行排序,然后轉(zhuǎn)換成字符串進(jìn)行比較。
2.比較兩個(gè)單獨(dú)的對(duì)象是否相等
let cmp = ( x, y ) => { // If both x and y are null or undefined and exactly the same if ( x === y ) { return true; } // If they are not strictly equal, they both need to be Objects if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) { return false; } //They must have the exact same prototype chain,the closest we can do is //test the constructor. if ( x.constructor !== y.constructor ) { return false; } for ( var p in x ) { //Inherited properties were tested using x.constructor === y.constructor if ( x.hasOwnProperty( p ) ) { // Allows comparing x[ p ] and y[ p ] when set to undefined if ( ! y.hasOwnProperty( p ) ) { return false; } // If they have the same strict value or identity then they are equal if ( x[ p ] === y[ p ] ) { continue; } // Numbers, Strings, Functions, Booleans must be strictly equal if ( typeof( x[ p ] ) !== "object" ) { return false; } // Objects and Arrays must be tested recursively if ( ! Object.equals( x[ p ], y[ p ] ) ) { return false; } } } for ( p in y ) { // allows x[ p ] to be set to undefined if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) { return false; } } return true; };
下面是StackOverflow大神封裝的方法,可以學(xué)習(xí)一下:
1.比較數(shù)組
// Warn if overriding existing method if(Array.prototype.equals) console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code."); // attach the .equals method to Array's prototype to call it on any array Array.prototype.equals = function (array) { // if the other array is a falsy value, return if (!array) return false; // compare lengths - can save a lot of time if (this.length != array.length) return false; for (var i = 0, l = this.length; i < l; i++) { // Check if we have nested arrays if (this[i] instanceof Array && array[i] instanceof Array) { // recurse into the nested arrays if (!this[i].equals(array[i])) return false; } else if (this[i] != array[i]) { // Warning - two different object instances will never be equal: {x:20} != {x:20} return false; } } return true; } // Hide method from for-in loops Object.defineProperty(Array.prototype, "equals", {enumerable: false});
2.比較對(duì)象
Object.prototype.equals = function(object2) { //For the first loop, we only check for types for (propName in this) { //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 (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) { return false; } //Check instance type else if (typeof this[propName] != typeof object2[propName]) { //Different types => not equal return false; } } //Now a deeper check using other objects property names for(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 (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) { return false; } else if (typeof this[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(!this.hasOwnProperty(propName)) continue; //Now the detail check and recursion //This returns the script back to the array comparing /**REQUIRES Array.equals**/ if (this[propName] instanceof Array && object2[propName] instanceof Array) { // recurse into the nested arrays if (!this[propName].equals(object2[propName])) return false; } else if (this[propName] instanceof Object && object2[propName] instanceof Object) { // recurse into another objects //console.log("Recursing to compare ", this[propName],"with",object2[propName], " both named \""+propName+"\""); if (!this[propName].equals(object2[propName])) return false; } //Normal value comparison for strings and numbers else if(this[propName] != object2[propName]) { return false; } } //If everything passed, let's say YES return true; }
總結(jié)
以上所述是小編給大家介紹的js比較兩個(gè)單獨(dú)的數(shù)組或?qū)ο笫欠裣嗟鹊膶?shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
JS常見(jiàn)問(wèn)題之為什么點(diǎn)擊彈出的i總是最后一個(gè)
最近有很多朋友問(wèn)我,為什么點(diǎn)擊彈出的i總是最后一個(gè),于是抽時(shí)間寫(xiě)了這篇文章,特此分享到腳本之家平臺(tái),供大家參考2016-01-01JS簡(jiǎn)單實(shí)現(xiàn)滑動(dòng)加載數(shù)據(jù)的方法示例
這篇文章主要介紹了JS簡(jiǎn)單實(shí)現(xiàn)滑動(dòng)加載數(shù)據(jù)的方法,涉及javascript事件響應(yīng)及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2017-10-10微信小程序?qū)崿F(xiàn)搜索框功能及踩過(guò)的坑
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)搜索框功能及踩過(guò)的坑,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06JS實(shí)現(xiàn)兩個(gè)大數(shù)(整數(shù))相乘
大數(shù),即超出語(yǔ)言所能表示的數(shù)字最大范圍的數(shù)字,那么如何實(shí)現(xiàn)兩個(gè)大數(shù)相乘呢?下面有個(gè)不錯(cuò)的方法,大家可以參考下2014-04-04詳解搭建es6+devServer簡(jiǎn)單開(kāi)發(fā)環(huán)境
這篇文章主要介紹了詳解搭建es6+devServer簡(jiǎn)單開(kāi)發(fā)環(huán)境,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09值得分享和收藏的xmlplus組件學(xué)習(xí)教程
值得分享和收藏的xmlplus組件學(xué)習(xí)教程,xmlplus是一個(gè)設(shè)計(jì)非常獨(dú)特 JavaScript 框架,用于快速開(kāi)發(fā)前后端項(xiàng)目,感興趣的小伙伴們可以參考一下2017-05-05Bootstrap實(shí)現(xiàn)input控件失去焦點(diǎn)時(shí)驗(yàn)證
這篇文章主要介紹了Bootstrap實(shí)現(xiàn)input控件失去焦點(diǎn)時(shí)驗(yàn)證的相關(guān)資料,非常不錯(cuò),需要的朋友可以參考下,需要的朋友可以參考下2016-08-08JavaScript中call,apply,bind的區(qū)別與實(shí)現(xiàn)
這篇文章主要介紹了JavaScript中call,apply,bind的區(qū)別與實(shí)現(xiàn),文章通過(guò)圍繞主題思想展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09