0

如何使用 打印网页中的对象数组元素值document.write

 <!DOCTYPE html> <html> <body>

 <script>

   var data =  [
           {
             "metadata" : {
                 "names":["times","values","types"],
                 "types":["time","linear","ordinal"]
             },
             "data": [
             ["1141237500000",4.216,"Global_active_power"],
             ["1141237560000",2.4130,"Global_active_power"],
             ["1141237620000",3.4230,"Global_active_power"],
             ["1141237680000",2.4560,"Global_active_power"],
             ["1141237500000",2.37130,"Voltage"],
             ["1141237560000",2.35840,"Voltage"],
             ["1141237620000",0.32690,"Voltage"],
             ["1141237680000",10.30980,"Voltage"],
             ["1141237500000",13.800,"Global_intensity"],
             ["1141237560000",16.400,"Global_intensity"],
             ["1141237620000",25.400,"Global_intensity"],
             ["1141237680000",13.800,"Global_intensity"],
           ],
           }
         ];

   document.write( data["data"] );

 </script> </body> </html>
4

1 回答 1

0

您可以使用JSON.stringify()将对象转换为字符串形式,可以使用document.write.

var data =  [
           {
             "metadata" : {
                 "names":["times","values","types"],
                 "types":["time","linear","ordinal"]
             },
             "data": [
             ["1141237500000",4.216,"Global_active_power"],
             ["1141237560000",2.4130,"Global_active_power"],
             ["1141237620000",3.4230,"Global_active_power"],
             ["1141237680000",2.4560,"Global_active_power"],
             ["1141237500000",2.37130,"Voltage"],
             ["1141237560000",2.35840,"Voltage"],
             ["1141237620000",0.32690,"Voltage"],
             ["1141237680000",10.30980,"Voltage"],
             ["1141237500000",13.800,"Global_intensity"],
             ["1141237560000",16.400,"Global_intensity"],
             ["1141237620000",25.400,"Global_intensity"],
             ["1141237680000",13.800,"Global_intensity"],
           ],
           }
         ];
             
var innerDataValues = data[0].data;

// document.write( JSON.stringify(data ) );
for( var i = 0; i < innerDataValues.length; i++ )
{
  var value = innerDataValues[i];
  document.write( value[0] + "<br />"); // 0 for timestamp
}

于 2016-07-21T09:34:27.297 回答