我是否在 javascript 中正确执行此迭代?

发布于 2024-09-15 10:15:06 字数 1638 浏览 3 评论 0原文

考虑下面的两个实现,

data.addRows([
  ['2004', 1000],
  ['2005', 1170],
  ['2006', 660],
  ['2007', 1030]
]);

上面的工作原理(即)它让我得到了我想要的。

我的实现是,

 for (var i = 0; i < 10; i++) {
        data.addRows['row'+i,i];
    }

这是一个有效的 for 循环还是我做错了什么?

我正在使用谷歌可视化 API 来绘制图表,但下面的答案不起作用,

http://jsbin.com /okafa3/edit 这是 http:// /code.google.com/apis/visualization/documentation/gallery/areachart.html

<html>
  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Year');
        data.addColumn('number', 'Sales');


       for (var i = 0; i < 10; i++) {
        data.addRows(['row'+i,i]);
    }

          var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                          hAxis: {title: 'Year', titleTextStyle: {color: '#FF0000'}}
                         });
      }
    </script>
  </head>
  <body>
    <div id="chart_div"></div>
  </body>
</html>​

Consider the two implementations below,

data.addRows([
  ['2004', 1000],
  ['2005', 1170],
  ['2006', 660],
  ['2007', 1030]
]);

The above works (ie) it gets me what i want.

and my implementation is,

 for (var i = 0; i < 10; i++) {
        data.addRows['row'+i,i];
    }

Is this a valid for loop or what am i doing wrong?

I am using google visualization api for drawing charts but the answers below doesn't work out,

http://jsbin.com/okafa3/edit which is a copy of http://code.google.com/apis/visualization/documentation/gallery/areachart.html

<html>
  <head>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Year');
        data.addColumn('number', 'Sales');


       for (var i = 0; i < 10; i++) {
        data.addRows(['row'+i,i]);
    }

          var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
                          hAxis: {title: 'Year', titleTextStyle: {color: '#FF0000'}}
                         });
      }
    </script>
  </head>
  <body>
    <div id="chart_div"></div>
  </body>
</html>​

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛滥成性 2024-09-22 10:15:06

完全相同的代码将是

var rows = [];
for (var i=0; i<10; i++) {
   rows.push(['row'+i, i]);
}
data.addRows(rows);

helios 建议的也可以工作,只不过它不是早期代码的精确表示,因为它会对 data.addRows 进行多次调用。

The exact equivalent code would be

var rows = [];
for (var i=0; i<10; i++) {
   rows.push(['row'+i, i]);
}
data.addRows(rows);

What helios had suggested would also work except it is not exact representation of earlier code as it would make more than one call to data.addRows.

疯狂的代价 2024-09-22 10:15:06

您缺少函数调用的括号:

 for (var i = 0; i < 10; i++) {
        data.addRows(['row'+i,i]);
    }

You're missing the parenthesis of the function call:

 for (var i = 0; i < 10; i++) {
        data.addRows(['row'+i,i]);
    }
或十年 2024-09-22 10:15:06

看来你的方法 addRows 需要一个 [string, int] 数组。因此,您需要执行以下操作:(

for (var i=0; i<10; i++) {
   data.addRows(   // parenthesis for calling a method
      [            // array of
           ['row'+i,i] // [string, int] even if it's only one
      ]);

为了清楚起见,添加了空格和输入)

It seems your method addRows needs an array of [string, int]. Thus you'll need to do:

for (var i=0; i<10; i++) {
   data.addRows(   // parenthesis for calling a method
      [            // array of
           ['row'+i,i] // [string, int] even if it's only one
      ]);

(Spaces and enters added for clarity)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文