简体   繁体   中英

Can't get console log - d3 JavaScript

I'm very new to using JavaScript and 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?

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

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:

Name,Age
John,30
Jane,32

Thanks!

It's because you're using d3 v5. Your csv reading needs to be written differently

Here's a question that shows how: How to load data from a CSV file in D3 v5

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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