簡體   English   中英

在網頁中沒有 [“{ and the }”] 的情況下,如何在 JavaScript console.log() 中打印出 [{ and }] ?

[英]How do you print out [{ and }] in JavaScript console.log() without the [“{ and the }”] in web page?

我在轉動輸入時遇到問題:

["apple", "banana", "carrot", "durian", "eggplant", "apple", "carrot"] 

進入正確的輸出:

[{ name: "Apple", count: 2 }, { name: "Banana", count: 1 }, { name: "Carrot", count: 2 }, { name: "Durian", count: 1 }, { name: "Eggplant", count: 1 }]

我遇到錯誤輸出的問題:

["{ name: "Apple", count: 2 }, { name: "Banana", count: 1 }, { name: "Carrot", count: 2 }, { name: "Durian", count: 1 }, { name: "Eggplant", count: 1 }"]. 

我怎樣才能有正確的輸出:

[{ name: "Apple", count: 2 }, { name: "Banana", count: 1 }, { name: "Carrot", count: 2 }, { name: "Durian", count: 1 }, { name: "Eggplant", count: 1 }] 

使用 console.log() 方法?

<html>
<body>

    <h1>Number of fruits 4</h1>
      
      <div id="output"></div>

     <script>
        
    var input = ["apple", "banana", "carrot", "durian", "eggplant", "apple", "carrot"];
        
        var A = 0;//to count the number of apples
        var B = 0;//to count the number of bananas
        var C = 0;//to count the number of carrots
        var D = 0;//to count the number of durians
        var E = 0; //to count the number of eggplants
   
         for (var i = 0; i < input.length; i++)
          {
             if (input[i] == "apple")
             {   
               A += 1;  
              }
             if (input[i] == "banana")
             {   
               B += 1;  
              }
             if (input[i] == "carrot")
             {   
               C += 1;  
              }
             if (input[i] == "durian")
             {   
               D += 1;  
             }
             if (input[i] == "eggplant")
             {   
               E += 1;  
             }
           }            
        
       var apple1 = '&quot;Apple&quot;';
       var banana1 = '&quot;Banana&quot;';
       var carrot1 = '&quot;Carrot&quot;';
       var durian1 = '&quot;Durian&quot;';
       var eggplant1 = '&quot;Eggplant&quot;';
 
       var x1 = '{ name: ' + apple1 + ', count: ' + A + ' }';
       var x2 = ', { name: ' + banana1 + ', count: ' + B + ' }';
       var x3 = ', { name: ' + carrot1 + ', count: ' + C + ' }';
       var x4 = ', { name: ' + durian1 + ', count: ' + D + ' }';
       var x5 = ', { name: ' + eggplant1 + ', count: ' + E + ' }'; 
      
         var res1 = x1.split();  
         var res2 = x2.split();  
         var res3 = x3.split();  
         var res4 = x4.split();  
         var res5 = x5.split();    

         var output = [ res1 + res2 + res3 + res4 + res5 ];        
       
        console.log("output = ", output);
        document.getElementById('output').innerHTML = JSON.stringify(output);

   </script>

   </body>
   </html>

您應該使用對象實體,例如:

let x1 = {
name: 'Apple',
count: A
};

和數組方法push (它將提供的元素添加到數組的末尾):

output.push(x1);

console.log(output); 將向您展示您的陣列。 但是要在頁面上顯示它,你應該像你一樣使用 JSON.stringify() 。

你提供的代碼真的很糟糕。 我最好閱讀更多的 JS 文檔 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM