我认为最简单的方法是像你尝试的那样将每个数组变成一个字符串,然后比较字符串。
要将数组转换为字符串,只需将此字符串方法放入数组即可。
这些是数组:
var arr1 = [1, 2, "foo", 3, "bar", 3.14]
var arr2 = [1, 2, "foo", 3, "bar", 3.14]
现在,您必须将它们转换为字符串。方法列表是:
arr1.toString().replace(/,/gi, "")
arr2.toString().replace(/,/gi, "")
方法做:
**.toString()**-
将数组转换为字符串,连接数组的元素。
例如。
["tree", "black hole"] -> "tree,black hole"
遗憾的是,它包含逗号。这就是为什么我们必须这样做:
***.replace(a, b)***
它会在你正在处理的字符串中找到第一个参数 (a) 并将其替换为第二个参数 (b)。
例如
"0000010000010000000".replace("1", "2")
将返回:"0000020000010000000"
它只替换了参数1的第一个实例,所以我们可以用正则表达式代替。
例如
"0000010000010000000".replace(/1/gi, "2")
将返回:"0000020000020000000"
你用 / 包裹你想要替换的东西。
说你要替换的是 1。你做到了:/1/。
但是你必须在最后添加 gi 以便它选择每个实例。
所以,你必须把 /1/gi 用逗号放在最后,然后你可以把你想用的替换它。
现在,您的两个数组是:
arr1:"12foo3bar3.14"
arr2:"12foo3bar3.14"
现在你这样说:
if(arr1 === arr2) {
// Now the code you put inside of this if statement will only run if arr1 and arr2 have the same contents.
} else {
// This code will run if arr1 and arr2 have any differences.
}
如果您想检查 arr1 是否包含 arr2 而不是具有相同的确切内容,请执行此操作。
if(arr1.indexOf(arr2) !== -1) {
//This code will happen if arr2 is inside of arr1. If there is one extra array
//item in arr1, it doesn't matter. But, if arr2 has an extra array item, nothing in
//this if will run. If you want arr2 to contain arr1, just make arr1 in the
//condition of this if arr2, and make arr2 arr1.
}
基本上,如果您希望数组完全相同,请执行以下操作:
if(arr1.toString().replace(/,/gi, "") === arr2.toString().replace(/,/gi, "")) {
//arrays are the same
} else {
//arrays are different
}
如果您想知道一个数组是否包含另一个数组,只需这样做:
arrayThatWillHoldAnotherArray = arrayThatWillHoldAnotherArray.toString().replace(/,/gi)
arrayThatWillBeInsideAnotherArray = arrayThatWillBeInsideAnotherArray.toString().replace(/,/gi)
if(arrayThatWillHoldAnotherArray.indexOf(arrayThatWillBeInsideAnotherArray) !== -1) {
//arrayThatWillHoldAnotherArray has arrayThatWillBeInsideAnotherArray inside of it
} else {
//it doesn't
}
console.log("Read the code to understand this.")
var arr1 = [1,2,"foo",3,"bar",3.14]
var arr2 = [1,2,"foo",3,"bar",3.14]
function checkIfArraysAreTheSame(a,b) {
if(a.toString().replace(/,/gi,"") === b.toString().replace(/,/gi,"")) {
console.log("A and B are the same!")
return true;
}
console.log("A and B are NOT the same!")
return false
}
checkIfArraysAreTheSame(arr1,arr2)
//expected output: A and B are the same!
//Now, let's add another item to arr2.
arr2.push("Lorem")
checkIfArraysAreTheSame(arr1,arr2)
//expected output: A and B are NOT the same!
function checkIfArrayIsNestedInsideAnother(a,b) {
//If this returns true, b is nested inside a.
if (a.toString().replace(/,/gi,"").indexOf(b.toString().replace(/,/gi,"")) > -1) {
console.log("B is nested inside of A!")
} else if(b.toString().replace(/,/gi,"").indexOf(a.toString().replace(/,/gi,"")) > -1) {
console.log("A is nested inside of B!")
}
}
checkIfArrayIsNestedInsideAnother(arr1, arr2)
//expected output: A is nested inside of B! because:
//arr1 (a): [1,2,"foo",3,"bar",3.14]
//arr2 (b): [1,2,"foo",3,"bar",3.14, "Lorem"]
//We added Lorem at line 15.
//Now, let's check if arr2 is nested inside arr1, which it is not.
checkIfArrayIsNestedInsideAnother(arr2, arr1)
//expected output: B is nested inside of A!