如何在javascript中创建动态数组

如何在javascript中创建动态数组,javascript,jquery,arrays,highcharts,Javascript,Jquery,Arrays,Highcharts,我通过初始化2d数组(系列),使用以下代码读取数据并加载二维数组 其中系列的结构如下(highchart系列) 系列:[{ 名称:“” 数据:[] }] 手动代码: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title> - jsFiddle demo</title

我通过初始化2d数组(系列),使用以下代码读取数据并加载二维数组

其中系列的结构如下(highchart系列)

系列:[{ 名称:“” 数据:[] }]

手动代码:

  <!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>


  <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>




  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>

  </style>




<script type='text/javascript'>
$(function () {

 var options = {
        chart :{
                  type: 'polygon',
                  renderTo: 'container',

        },
        title: {
            text: 'Height vs Weight'
        },
        yAxis: {
            title: false,
            gridLineWidth:0,
            lineWidth:0,
            labels:{
                enabled: false
            }
        },
          series: [{},{}]
    };

    $.getJSON('data.json', function(data) {
        options.series[0].data = data[0];
        options.series[1].data = data[1];

        var chart = new Highcharts.Chart(options);
    })

});
</script>

</head>
<body>
  <script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>


</body>
series: [[]]

 $.getJSON('data.json', function(data) {
        for (i=0; i<data.length(); i++) {
           series[i].data = data[i];
         }
    })
动态代码:

  <!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>


  <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>




  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <style type='text/css'>

  </style>




<script type='text/javascript'>
$(function () {

 var options = {
        chart :{
                  type: 'polygon',
                  renderTo: 'container',

        },
        title: {
            text: 'Height vs Weight'
        },
        yAxis: {
            title: false,
            gridLineWidth:0,
            lineWidth:0,
            labels:{
                enabled: false
            }
        },
          series: [{},{}]
    };

    $.getJSON('data.json', function(data) {
        options.series[0].data = data[0];
        options.series[1].data = data[1];

        var chart = new Highcharts.Chart(options);
    })

});
</script>

</head>
<body>
  <script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/highcharts-more.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>


</body>
series: [[]]

 $.getJSON('data.json', function(data) {
        for (i=0; i<data.length(); i++) {
           series[i].data = data[i];
         }
    })
系列:[[]]
$.getJSON('data.json',函数(数据){
对于(i=0;i您需要使用函数动态追加

使用第二个数组编辑,以获得所需的“数据”结构

// initialize array
var series = [];


$.getJSON('data.json', function(data) {
    var temparray = [];
    for (i=0; i<data.length; i++) {
        temparray["data"] = data[i];
        series.push(temparray);
    }
})
//初始化数组
var系列=[];
$.getJSON('data.json',函数(数据){
var temparray=[];

对于(i=0;iAnd,
.length
是一个属性!谢谢,错过了这个属性。只需使用这个:
series.push({name:,data:data[i]});
用您想要的数据结构编辑了我的答案。