您好,欢迎访问一九零五行业门户网

如何使用jQuery/JavaScript比较两个JavaScript数组对象?

在javascript中,数组是一个带有索引作为键和数组值作为数组对象特定键的值的对象。有时,我们需要检查两个数组是否相同。
我脑海中首先想到的解决方案是使用等号操作符,像array1 == array2这样进行比较。哎呀!这样是行不通的,因为数组是一个对象,在javascript中我们不能直接比较两个对象。所以,我们必须比较数组的每个元素。
在本教程中,我们将学习使用不同的方法来比较两个javascript数组对象。
use the sort() method of javascript and compare every elementsort()方法允许我们在javascript中对数组值进行排序。之后,我们可以使用for循环来比较数组中每个索引处的元素。如果任何索引处的元素不匹配,我们可以说这两个数组对象是不同的。
syntax用户可以按照以下语法使用sort()方法和for循环来比较两个数组对象。
// sort arrays firstarra1.sort();array2.sort(); if (array1.length != array2.length) { // arrays are not the same } else { for () { // if elements at index i are not the same, return false } }}// both arrays are the same
algorithm用户可以按照以下算法进行操作。
step 1 − use the sort() method to sort both arrays.
步骤2 - 比较两个数组的长度;如果长度不相同,则返回false,表示两个数组不相同。
步骤 3 − 如果两个数组的长度相同,则使用for循环遍历两个数组。
step 4 − compare the elements of both arrays at every index, and if the elements at the index don’t match, return false.
步骤 5 - 如果两个数组中的所有元素都匹配,则两个数组相同。
example在下面的示例中,我们创建了两个数字数组,并使用sort()方法对它们进行排序。然后,我们使用for循环来比较两个数组的每个元素。
in the output, users can see that both arrays are the same as both arrays contain the same values.
<html><body> <h3>using the <i>array.sort()</i> method and <i>for</i> loop to check if two arrays are the same using javascript </h2> <button onclick = checkarray()> compare arrays </button> <div id = output> </div> <script> let output = document.getelementbyid('output'); let array1 = [32, 32, 54, 1, 2, 3, 4]; let array2 = [1, 2, 3, 4, 32, 54, 32]; output.innerhtml += the first array values are + array1 + <br>; output.innerhtml += the second array values are + array2 + <br>; function checkarray() { array1.sort(); array2.sort(); if (array1.length != array2.length) { output.innerhtml += both arrays are not same!; return false; } else { for (let i = 0; i < array1.length; i++) { if (array1[i] != array2[i]) { output.innerhtml += both arrays are not same!; return false; } } } output.innerhtml += both arrays are the same!; return true; } </script></body></html>
使用foreach循环和indexof()方法we can use the foreach loop to iterate through every array element. the indexof() method finds the first occurrence of the element in the array and returns -1 if the reference array doesn’t contain the element.
syntax用户可以按照以下语法使用foreach循环和indexof()方法来比较两个数组对象。
if (array2.length != array1.length) { // array are not the same return false;} else { array1.foreach((element) => { if (array2.indexof(element) == -1) { return false; } })}
algorithmin this algorithm, we don’t need to sort the arrays like the first approach.
步骤 1 - 检查两个数组的长度是否相同;如果不相同,则返回 false。
step 2 − if the lengths are the same, use the foreach() loop to iterate through every element.
步骤 3 − 对于数组1的每个元素,使用indexof()方法检查其是否存在于数组2中。
第四步 - 如果indexof()方法对于任何一个元素返回-1,则意味着两个数组不相同。
examplein the example below, when the user clicks the button, it will invoke a comparearray() function. the comparearray() function compares the two arrays of string elements.
在输出中,用户可以观察到两个数组不相同,因为两个数组的值是不同的。
<html><body> <h3>using the <i>foreach() loop and indexof() method</i> to check if two arrays are the same using javascript </h3> <button onclick = comparearray()> compare arrays</button> <div id = output> </div> <script> let output = document.getelementbyid('output'); let array1 = [hello, hi, how]; let array2 = [are, you, !]; output.innerhtml += the first array values are + array1 + <br>; output.innerhtml += the second array values are + array2 + <br>; function comparearray() { var issame = true; if (array2.length != array1.length) { output.innerhtml += both arrays are not same!; return false; } else { array2.foreach((element) => { if (array1.indexof(element) == -1) { issame = false; } }) } if (issame) { output.innerhtml += both arrays are the same!; } else { output.innerhtml += both arrays are not same!; } return true; } </script></body></html>
我们已经学习了两种不同的方法来比较javascript中的两个数组。用户可以使用第一种方法来比较包含重复值的数组,而第二种方法只适用于比较包含唯一值的数组。
also, users can use the json.stringify() method to compare the array of objects and sorted arrays.
以上就是如何使用jquery/javascript比较两个javascript数组对象?的详细内容。
其它类似信息

推荐信息