国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

js比較兩個單獨的數組或對象是否相等的實例代碼

2024-05-06 15:40:06
字體:
來源:轉載
供稿:網友

所謂js的中的傳值,其實也就是說5種基本數據類型(null,undefind,boolean,number,string)

傳引用也就是說的那個引用數據類型,(array和object)

基本數據類型的值不可變,而引用數據類型的值是可變的

所以當你比較數組和對象時,都是false;除非你是克隆的原份數據

即: var a = { name: "李四" }; var b = a;

大家通常稱對象為引用類型,以此來和基本類型進行區分; 而對象值都是引用,所以的對象的比較也叫引用的比較,當且當他們都指向同一個引用時,即都引用的同一個基對象時,它們才相等.

1.比較兩個單獨的數組是否相等

JSON.stringify(a1) == JSON.stringify(a2)

a1.toString() == a2.toString()

要判斷2個數組是否相同,把數組轉換成字符串進行比較。

如果要比較兩個數組的元素是否相等,則:

JSON.stringify([1,2,3].sort()) === JSON.stringify([3,2,1].sort());

[1,2,3].sort().toString() === [3,2,1].sort().toString();

判斷2個數組是否相同,首先要把數組進行排序,然后轉換成字符串進行比較。

2.比較兩個單獨的對象是否相等

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大神封裝的方法,可以學習一下:

1.比較數組

// Warn if overriding existing methodif(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 arrayArray.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 loopsObject.defineProperty(Array.prototype, "equals", {enumerable: false});            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 新泰市| 临清市| 密云县| 昌邑市| 徐州市| 彰武县| 天门市| 新兴县| 吕梁市| 清远市| 祁门县| 西林县| 灌阳县| 孝昌县| 利川市| 共和县| 巴彦淖尔市| 大足县| 乡城县| 哈巴河县| 资中县| 通城县| 阳东县| 枣强县| 寿阳县| 报价| 青州市| 镇康县| 闻喜县| 道孚县| 朝阳市| 凌源市| 霍林郭勒市| 台安县| 永平县| 丘北县| 咸丰县| 启东市| 盘锦市| 长海县| 芦溪县|