xpin1509 / xpin1509.github.io

github pages
1 stars 0 forks source link

如何验证两个数组相等 #10

Open xpin1509 opened 6 years ago

xpin1509 commented 6 years ago

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

xpin1509 commented 6 years ago

转为字符串(有坑) JSON.stringify(a1) == JSON.stringify(a2) a1.toString() == a2.toString()

xpin1509 commented 6 years ago

判断全相等价于判断其中一个不等 function isAllEqual(array){ if(array.length>0){ return !array.some(function(value,index){ return value !== array[0]; });
}else{ return true; } }

niaogege commented 6 years ago

先占坑!!斌哥,我要pull request