SHARE
TWEET

Untitled

a guest Jun 26th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var isEqual = function (value, other) {
  2.  
  3.     // Get the value type
  4.     var type = Object.prototype.toString.call(value);
  5.  
  6.     // If the two objects are not the same type, return false
  7.     if (type !== Object.prototype.toString.call(other)) return false;
  8.  
  9.     // If items are not an object or array, return false
  10.     if (['[object Array]', '[object Object]'].indexOf(type) < 0) return false;
  11.  
  12.     // Compare the length of the length of the two items
  13.     var valueLen = type === '[object Array]' ? value.length : Object.keys(value).length;
  14.     var otherLen = type === '[object Array]' ? other.length : Object.keys(other).length;
  15.     if (valueLen !== otherLen) return false;
  16.  
  17.     // Compare two items
  18.     var compare = function (item1, item2) {
  19.  
  20.         // Get the object type
  21.         var itemType = Object.prototype.toString.call(item1);
  22.  
  23.         // If an object or array, compare recursively
  24.         if (['[object Array]', '[object Object]'].indexOf(itemType) >= 0) {
  25.             if (!isEqual(item1, item2)) return false;
  26.         }
  27.  
  28.         // Otherwise, do a simple comparison
  29.         else {
  30.  
  31.             // If the two items are not the same type, return false
  32.             if (itemType !== Object.prototype.toString.call(item2)) return false;
  33.  
  34.             // Else if it's a function, convert to a string and compare
  35.             // Otherwise, just compare
  36.             if (itemType === '[object Function]') {
  37.                 if (item1.toString() !== item2.toString()) return false;
  38.             } else {
  39.                 if (item1 !== item2) return false;
  40.             }
  41.  
  42.         }
  43.     };
  44.  
  45.     // Compare properties
  46.     if (type === '[object Array]') {
  47.         for (var i = 0; i < valueLen; i++) {
  48.             if (compare(value[i], other[i]) === false) return false;
  49.         }
  50.     } else {
  51.         for (var key in value) {
  52.             if (value.hasOwnProperty(key)) {
  53.                 if (compare(value[key], other[key]) === false) return false;
  54.             }
  55.         }
  56.     }
  57.  
  58.     // If nothing failed, return true
  59.     return true;
  60.  
  61. };
  62.  
  63.  
  64. /** Usage **/
  65. var arr1 = [1, 2, 3, 4, 5];
  66. var arr2 = [1, 2, 3, 4, 5];
  67. isEqual(arr1, arr2); // returns true
  68.  
  69. var arrObj1 = [1, 2, {
  70.     a: 1,
  71.     b: 2,
  72.     c: 3
  73. }, 4, 5];
  74. var arrObj2 = [1, 2, {
  75.     c: 3,
  76.     b: 2,
  77.     a: 1
  78. }, 4, 5];
  79. isEqual(arrObj1, arrObj2); // returns true
  80.  
  81. var arr1 = [1, 2, 3, 4, 5];
  82. var arr3 = [5, 4, 3, 2, 1];
  83. isEqual(arr1, arr3); // returns false
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top