简体   繁体   English

无法获取控制台日志-D3 JavaScript

[英]Can't get console log - d3 JavaScript

I'm very new to using JavaScript and d3. 我对使用JavaScript和d3非常陌生。 I got this example from a tutorial, but can't get it to log the items to the console. 我从一个教程中获得了这个示例,但是无法将其记录到控制台中。 Does anyone see something wrong with this HTML file? 有人看到这个HTML文件有问题吗?

This is it in its entirety: 这是全部内容:

<meta charset="utf-8"/>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
d3.csv("data/employee.csv", function(data) {
    for (var i = 0; i < data.length; i++) {
            console.log(data[i].Name);
            console.log(data[i].Age);
        }
});
</script>

If i swap out the for loop for 如果我换出for循环

console.log(data);

I do see the object details in the console: 我在控制台中确实看到了对象的详细信息:

控制台中的对象

However, with the original snippet above the console shows nothing. 但是,使用控制台上方的原始代码片段不会显示任何内容。 Here is my CSV, any help would be appreciated: 这是我的CSV,任何帮助将不胜感激:

Name,Age
John,30
Jane,32

Thanks! 谢谢!

It's because you're using d3 v5. 这是因为您使用的是d3 v5。 Your csv reading needs to be written differently 您的csv阅读内容需要写成不同的形式

Here's a question that shows how: How to load data from a CSV file in D3 v5 这是一个显示方式的问题: 如何在D3 v5中从CSV文件加载数据

This should work for you: 这应该为您工作:

<meta charset="utf-8"/>

<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
  d3.csv('data/employee.csv')
    .then(function(data) {
        for (var i = 0; i < data.length; i++) {
                console.log(data[i].Name);
                console.log(data[i].Age);
            }
    })
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM