最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2019年04月28日 08:33:41   作者:消逝的風i  
本文通過實例代碼給大家介紹了js比較兩個單獨的數組或對象是否相等 ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

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

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

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

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

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

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

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 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.比較對象

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;
}

總結

以上所述是小編給大家介紹的js比較兩個單獨的數組或對象是否相等的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

相關文章

  • JavaScript中的細節(jié)分析

    JavaScript中的細節(jié)分析

    高山登不上,不是因為體力不支,只因鞋里一粒。學習也是如此,因而有必要把JavaScript中常見的和與其它語言不同的那些細節(jié)學習一下
    2012-06-06
  • JavaScript function 的 length 屬性使用介紹

    JavaScript function 的 length 屬性使用介紹

    函數的 length 得到的是形參個數,如果函數內部是通過arguments 調用參數,而沒有實際定義參數的話, length 只會的得到0
    2014-09-09
  • uniapp單頁面實現頁面切換的使用示例

    uniapp單頁面實現頁面切換的使用示例

    本文主要介紹了uniapp單頁面實現頁面切換的使用示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • 詳解JavaScript作用域、作用域鏈和閉包的用法

    詳解JavaScript作用域、作用域鏈和閉包的用法

    這篇文章主要介紹了JavaScript作用域、作用域鏈和閉包的用法,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下
    2020-09-09
  • js讀取注冊表的鍵值示例

    js讀取注冊表的鍵值示例

    讀取注冊表的鍵值的方法有很多,在本文將為大家詳細介紹下使用js如何實現注冊表的鍵值讀取,感興趣的朋友可以參考下
    2013-09-09
  • cropper js基于vue的圖片裁剪上傳功能的實現代碼

    cropper js基于vue的圖片裁剪上傳功能的實現代碼

    這篇文章主要介紹了cropper js基于vue的圖片裁剪上傳功能的相關資料,需要的朋友可以參考下
    2018-03-03
  • Layer彈出層動態(tài)獲取數據的方法

    Layer彈出層動態(tài)獲取數據的方法

    今天小編就為大家分享一篇Layer彈出層動態(tài)獲取數據的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 簡單實現js浮動框

    簡單實現js浮動框

    這篇文章主要為大家詳細介紹了如何簡單實現js浮動框,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • javascript中的parseInt和parseFloat區(qū)別

    javascript中的parseInt和parseFloat區(qū)別

    這篇文章用簡單的小例子演示了parseInt和parseFloat區(qū)別,有需要的朋友可以參考一下
    2013-07-07
  • 微信小程序h5頁面跳轉小程序的超詳細講解

    微信小程序h5頁面跳轉小程序的超詳細講解

    開發(fā)中涉及到一個需求,就是從一個預約票購買的頁面需要跳轉到?小程序,下面這篇文章主要給大家介紹了關于微信小程序h5頁面跳轉小程序的超詳細講解,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02

最新評論

离岛区| 石棉县| 吉林市| 开江县| 兰州市| 平利县| 贡山| 陈巴尔虎旗| 福鼎市| 大方县| 兴海县| 宁河县| 柞水县| 全州县| 肥城市| 缙云县| 靖江市| 东辽县| 洛浦县| 丹江口市| 方城县| 苏尼特左旗| 东丽区| 江达县| 潢川县| 额济纳旗| 新和县| 青州市| 丹东市| 枝江市| 手游| 扎囊县| 阿拉善左旗| 桦川县| 敦煌市| 衡阳市| 甘德县| 手游| 当涂县| 延川县| 漳州市|