使用for循环在两个数组中查找相同的元素,没有任何函数

时间:2018-03-01 12:39:35

标签: javascript arrays element

我有两个数组arr1arr2我希望我的输出像1,2,3,4 ...如果我的arr1arr2包含相同的号码,但不幸的是我只得到一场比赛 例如,如果我放var arr1 = [1,2,3,4,5,6,7,8,9,0]& var arr2 = [ 1,2,3,4,5]然后我希望输出如下:

  
    

1,2,3,4,5

  

        var i, j;
        var arr1 = new Array(5);
        var arr2 = new Array(5);

        for (i = 0; i <= 4; i++) {
            arr1[i] = parseInt(prompt("Enter The arr1 Element"));
        }

        for (i = 0; i <= 4; i++) {
            arr2[i] = parseInt(prompt("Enter The Arr2 Element"));
        }
    
            var k;
            var l;
            for (k = 0; k < arr1.length; k++) {
                for (l = 0; l < arr2.length; l++) {
                    if (arr1[k] == arr2[l]) {
                        document.getElementById("show").innerHTML = arr1[k];
                       
                    }
                    continue;
                }
            }
        
  
<html>

<head>
    <title>
        Common In Array[]
    </title>
</head>

<body>
   
    <p id="show"></p>
    
</body>

</html>

5 个答案:

答案 0 :(得分:0)

  

然后我想要输出如:1,2,3,4,5

您只使用赋值运算符 =设置一个值。

document.getElementById("show").innerHTML = arr1[k];

您需要在数组中推送您的值,然后在使用,

分隔后立即显示它们
var output = [];
for (k = 0; k < arr1.length; k++) {
  for (l = 0; l < arr2.length; l++) {
    if (arr1[k] == arr2[l]) {
      output.push(arr1[k]);
    }
  }
}
document.getElementById("show").innerHTML = output.join(",");

<强>演示

&#13;
&#13;
var i, j;
var arr1 = new Array(5);
var arr2 = new Array(5);

for (i = 0; i <= 4; i++) {
  arr1[i] = parseInt(prompt("Enter The arr1 Element"));
}

for (i = 0; i <= 4; i++) {
  arr2[i] = parseInt(prompt("Enter The Arr2 Element"));
}

var k;
var l;
var output = [];
for (k = 0; k < arr1.length; k++) {
  for (l = 0; l < arr2.length; l++) {
    if (arr1[k] == arr2[l]) {
      output.push(arr1[k]);
    }
  }
}
document.getElementById("show").innerHTML = output.join(",");
&#13;
<p id="show"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

检查一下。你不需要非常复杂的逻辑来实现你想要的东西

&#13;
&#13;
    var arr1 = [1,2,3,4,5,6,7,8,9,0];
    var arr2 = [ 1,2,3,4,5];

    let res = [];

    if(arr1.length >= arr2.length) {
      res = arr1.filter(a => arr2.find(o => o === a));
    } else {
      res = arr2.filter(a => arr1.find(o => o === a));
    }

    console.log(res)
    document.getElementById("show").innerHTML = res;
&#13;
<p id="show"></p>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个:

var outputstring = "";
for (k = 0; k < arr1.length; k++) {
    for (l = 0; l < arr2.length; l++) {
        if (arr1[k] == arr2[l]) {
            outputstring += arr1[k] + ",";
        }
        continue;
    }
}
document.getElementById("show").innerHTML = outputstring;

答案 3 :(得分:0)

您可以使用过滤方法并比较两个数组...

&#13;
&#13;
       
let arr1 = [1,2,3,4,5];
let arr2 = [1,2,3,6,7];
         
let result = arr1.filter((element)=>arr2.includes(element));   
console.log(result);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

&#13;
&#13;
var array1 = [1,2,3,4,5,6,7,8,9,0];
var array2 = [1,2,3,4,5];

var results = array1.filter(function(item) {
    return array2.indexOf(item) !== -1;
});

console.log(results);
&#13;
&#13;
&#13;