I've been trying to learn the basics of d3. I've run into some difficulty getting data from an external source. I've had functioning charts before where the data is put in directly as an JSON object or array, but I'm trying to work with a CSV right now. I'm trying to push the data into an array of objects, and while it seems to work partially, something strange is going on with the array that I'm unable to access the information inside it:
var testArray = [
{name: "John",
height: "Six Feet"},
{name: "Karl",
height: "Seven Feet"}
]
console.log(testArray);
console.log(testArray[0].name)
var dataset = [];
d3.csv("../scripts/books.csv", function(data) {
dataset.push( {
title: data.Title,
author: data.Author
});
});
console.log(dataset);
console.log(dataset[0].title);
testArray is just used as example. When I log testArray[0].name it correctly returns "John" - but when I try the same format on dataset, it says "Cannot read property 'title' of undefined." I've tried with pure bracket notation and pushing the data in other formats, but nothing seems to work.
Some of the d3 guides suggest just doing everything you need to create your chart within the callback loop, but the way I learned and the way many online classes teach is to have the data separated out and build your own functions and loops to create the chart. I'm having difficulty reconciling these things. I do NOT want to build everything into the callback function, if at all possible.