数组打印多次

时间:2018-01-30 08:05:15

标签: javascript html arrays

我正在尝试打印多次出现的数组中的每个元素,以及它们在哪个索引上。 例如:Tall(Number):2,index:0 但是,我只希望它为每个索引打印一次。很抱歉这个错误的解释并且它是挪威语!

<!Doctype html>
<HTML lang="no">

<head>
    <meta charset="utf-8">
    <title>#</title>
    <script>
        window.onload = oppstart;

        function oppstart() {
            var heltall = [2, 3, 4, 3, 5, 2, 1, 4, 2, 1, 4, 2, 3, 4, 5, 6];
            var utskrift = "-- \n";

            for (j = 0; j < heltall.length; j++) {
                var talletViSkalTelle = heltall[j];
                for (i = 0; i < heltall.length; i++) {
                    if (heltall[i] == talletViSkalTelle) {
                        utskrift += "Tall: " + talletViSkalTelle + ", index: " + i + "\n";
                    }
                }
            }
            print(utskrift);
        }

        function print(text) {
            var el = document.getElementById("utskrift");
            el.innerText = text;
            //el = element//
        }

    </script>
</head>

<body>

<p id="utskrift"></p>

</body>

3 个答案:

答案 0 :(得分:1)

创建一个存储已经过的数字的数组,然后在再次迭代之前检查该数组是否已经存储了下一个数字。

请参阅下面的工作代码段

<!Doctype html>
<HTML lang="no">

<head>
    <meta charset="utf-8">
    <title>#</title>
    <script>
        window.onload = oppstart;
        var pushed = [];

        function oppstart() {
            var heltall = [2, 3, 4, 3, 5, 2, 1, 4, 2, 1, 4, 2, 3, 4, 5, 6];
            var utskrift = "-- \n";

            for (j = 0; j < heltall.length; j++) {
                var talletViSkalTelle = heltall[j];
                if(!check(talletViSkalTelle)){
                  pushed.push(talletViSkalTelle);
                  for (i = 0; i < heltall.length; i++) {
                      if (heltall[i] == talletViSkalTelle) {
                          utskrift += "Tall: " + talletViSkalTelle + ", index: " + i + "\n";
                      }
                  }
                }
            }
            print(utskrift);
        }

        function print(text) {
            var el = document.getElementById("utskrift");
            el.innerText = text;
            //el = element//
        }
        
        function check(num){
          for(var i = 0; i<pushed.length; i++){
            if(num === pushed[i]){
              return true;
            }
          }
          return false;
        }

    </script>
</head>

<body>

<p id="utskrift"></p>

</body>

答案 1 :(得分:0)

我建议你创建一个数组来存储你已经处理过的数字,让我们说:

var tallerViHarTellet = []
初始化heltall数组后

然后,检查号码是否已经过检查,如下所示:

if (tallerViHarTellet.includes(talletViSkalTelle))continue;
tallerViHarTellet.push(tallerViSkalTelle)

var talletViSkalTelle = heltall[j];

这样,您将跳过检查已检查过的数字。

答案 2 :(得分:0)

是否需要分别打印所有数组重复元素并使用不同的索引?或者您需要重复具有它们出现的所有索引的数组元素?

<!Doctype html>
<HTML lang="no">

<head>
    <meta charset="utf-8">
    <title>#</title>
    <script>
        window.onload = oppstart;

        function getIndices (arr){
          var out = {};
          arr.map((tall, idx) => {
              if(out.hasOwnProperty(tall)) {
                  out[tall].push(idx)
              } else { 
                  out[tall] = [idx]
              }
          })
          return out;
        }

        function oppstart() {
            var heltall = [2, 3, 4, 3, 5, 2, 1, 4, 2, 1, 4, 2, 3, 4, 5, 6];
            var utskrift = "-- \n";
            var res = getIndices(heltall);

            Object.keys(res).map(function(tall){
            if (res[tall].length > 1) {
                utskrift += "Tall: " + tall + ", indices: " + res[tall].join(', ') + "\n";
            }
            })
            print(utskrift);
        }

        function print(text) {
            var el = document.getElementById("utskrift");
            el.innerText = text;
        }

    </script>
</head>

<body>

<p id="utskrift"></p>

</body>

OutPut

-- 
Tall: 1, indices: 6, 9
Tall: 2, indices: 0, 5, 8, 11
Tall: 3, indices: 1, 3, 12
Tall: 4, indices: 2, 7, 10, 13
Tall: 5, indices: 4, 14