0

There is something I don't understand with d3. Let's say you have a json file, you're loading it like this :

var url = 'http://localhost:5000/authors';
d3.json('http://url.com', function(data) {
nestedData(data);
 });

then you have your function like this :

var nestedData = function(data) {
    d3.nest()
    .key(function(d) { return d.stuff;})
    .rollup(function(d) { return d3.sum(d, function(g) {return g.otherstuff;}); })
    .entries(data);
}

If I have understood well the principle of the callback function, my function "nedtedData" will be executed, only once data are loaded. How can I retrieve the data inside my function named "nestedData ? Let's say I want to push the data into a empty variable (var newdata = []) ?

My main concern with this is to try to understand asynchronous callback and data set behaviors.

Simon Breton
  • 2,638
  • 7
  • 50
  • 105
  • 1
    *"How can I retrieve the data inside my function named "nestedData ?"* You already have access to it via the `data` parameter. Not sure what you are confused about? – Felix Kling May 25 '16 at 16:30
  • Thanks Felix, I'll try to ask differently : How can I see, from my javascript console what's in my nestedData variable ? – Simon Breton May 25 '16 at 16:58
  • I would set a breakpoint in the code (e.g. via `debugger`) and then inspect `data`. – Felix Kling May 25 '16 at 16:59
  • How do I do this ? I've look for it but nothing clear... – Simon Breton May 25 '16 at 17:04
  • See https://developers.google.com/web/tools/chrome-devtools/debug/breakpoints/add-breakpoints for Chrome. Other browsers have similar functionality. – Felix Kling May 25 '16 at 17:06
  • Ok thanks. However It's not the answers I really wanted but I guess I have to rethink my question. – Simon Breton May 25 '16 at 17:10
  • You can also simply add a `console.log(data)` to your function is you want to see the value in the console. *"I have to rethink my question"* that might help :) – Felix Kling May 25 '16 at 17:11
  • actually @FelixKling can you answers the second part of my question : how do I push data inside NestedData into a global variable ? – Simon Breton May 25 '16 at 17:40
  • The same way you add a value to any other variable: `myGlobalVariable = data;` or `myGlobalVariable.push(data)`, depending what exactly you want to do. But that won't be useful if you don't know *when* the data was assigned. I believe you should have a look at [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/q/23667086/218196). – Felix Kling May 25 '16 at 17:46
  • Yep you're right. I'm going to read this thanks. actually I know how to pass data into myglobalvariable and it's work. However when I'm using it in another function (to build my table), myglobalvariable is empty. I guess is for the async stuff. I guess I start understand the problem but not enough to solve it ! – Simon Breton May 25 '16 at 18:11
  • Hopefully after reading the linked question it will be clearer. – Felix Kling May 25 '16 at 18:17

0 Answers0