要在JavaScript中比较数组,请尝试运行以下代码。 来自两个数组的匹配元素在此处显示:
<html> <body> <script> var arr1 = [50,60,65,90]; var arr2 = [25,35,50,90]; for (i = 0; i < arr1.length; i++) { for (z = 0; z < arr1.length; z++) { if (arr1[i] === arr2[z]) { document.write("<br>Matched element: "+arr2[z]); } } } </script> </body> </html>
输出结果
Matched element: 50 Matched element: 90