JavaScript 如何查看结构的数组

JavaScript 如何查看结构的数组

JavaScript的数组是异构的。数组的结构相同,两个方括号之间是封闭的 [ ] ,字符串应该用 “双引号”‘单引号’ 括起来。您可以通过使用JSON.stringify()来获得结构。对于其他过程,请查看此链接。

语法:

JSON.stringify(value, replacer, space)
JavaScript

alert()功能是在继续进行之前为用户提供信息。因此,在下面的代码中,点击按钮 function(Alert()) 后,有两个警报(alert()),显示所有警报后,网页继续进行。点击警报框中的 ‘ok’ ,会显示下一个警报,直到所有警报都完成。

让我们检查可以应用的警报类型:

  • alert(JSON.stringify(guardians)): 它以数组结构显示。
  • alert(JSON.stringify(guardians, 9, 1)): 它显示定制结构,其中 9 作为替代者,使数组元素以垂直方式打印,并且 1 作为空间号,在元素之间提供一个空间。
  • alert(JSON.stringify(guardians, “”, 5)): 它显示定制结构,其中 “” 作为替代者,使数组元素以垂直方式打印,并且 5 作为空间号,在元素之间提供五个空间。

示例1:

<h1 style="color:Green;"> 
    GeeksforGeeks 
</h1> 
  
<h3> 
    Getting array structure JSON.stringify 
</h3> 
  
<p id="d1"></p> 
  
  
  
<input type="button" onclick="Alert()" value="Click Here" /> 
  
<script> 
    function Alert() { 
          
        // Array structure 
        var guardians = ["Quill", "Gamora", 
                "Groot", "Rocket", "Drax", 21]; 
          
        // Prints the array elements with a comma 
        // separated but not the structure 
        document.getElementById("d1").innerHTML 
                = "Guardians = " + guardians; 
          
        // Alerts the window with the array 
        alert(JSON.stringify(guardians)) 
          
        // Alerts the window with a customized array 
        alert(JSON.stringify(guardians, 9, 1)) 
    } 
</script>
HTML

输出:

JavaScript 如何查看结构的数组

示例 2: 您还可以轻松获取关联数组的结构。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        How to view array of a 
        structure in JavaScript? 
    </title> 
</head> 
  
<body style="text-align:center;"> 
      
    <h1 style="color:Green;"> 
        GeeksforGeeks 
    </h1> 
      
    <h3> 
        Getting associate array 
        structure JSON.stringify 
    </h3> 
  
    <p id="d1"></p> 
  
  
  
    <input type="button" onclick="Alert()"
                value="Click Here" /> 
  
    <script> 
        function Alert() { 
          
            // Array structure 
            var guardians = {  
                "Newton": "Gravity",  
                "Albert": "Energy",  
                "Edison": "Bulb",  
                "Tesla": "AC" 
            }; 
              
            // Alerts the window with the array 
            alert(JSON.stringify(guardians)) 
              
            // Alerts the window with a 
            // customized array 
            alert(JSON.stringify(guardians, 9, 1)) 
              
            // Alerts the window with a 
            // customized array 
            alert(JSON.stringify(guardians,"",5)) 
        } 
    </script> 
</body> 
  
</html>            
HTML

输出:

JavaScript 如何查看结构的数组

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册