繁体   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