JavaScript比较两个数组的内容是否相同(推荐)
程序员文章站
2022-06-29 12:44:39
今天意外地发现javascript是不能用==或===操作符直接比较两个数组是否相等的。
alert([]==[]); // false
alert([]==...
今天意外地发现javascript是不能用==或===操作符直接比较两个数组是否相等的。
alert([]==[]); // false alert([]===[]); // false
以上两句代码都会弹出false。
因为javascript里面array是对象,==或===操作符只能比较两个对象是否是同一个实例,也就是是否是同一个对象引用。目前javascript没有内置的操作符判断对象的内容是否相同。
但是惯性思维让人以为数组也是值,是可以比较的。
如果要比较数组是否相等,就只能遍历数组元素比较。
在网上流传很普遍的一种做法是将数组转换成字符串:
json.stringify(a1) == json.stringify(a2)
或
a1.tostring() == a2.tostring()
请不要使用这种方法。
这种方法在某些情况下是可行的,当两个数组的元素顺序相同且元素都可以转换成字符串的情况下确实可行,但是这样的代码存有隐患,比如数字被转换成字符串,数字“1”和字符串“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});
大神还顺手给了比较object的方法:
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; }
以上所述是小编给大家介绍的javascript比较两个数组的内容是否相同(推荐),希望对大家有所帮助