1

I have a variable Params passed in as an argument to the makePlot function. I would like to access it within the d3.json request but it is coming up as undefined. See Below.

const makePlot = (Params) => {
  // Params defined here
  d3.json(`somedata.json`, (error, data) => {
  // Params not defined here
    data.forEach((d) => {
     d.Params.something = Number(d.Params.something);
     d.Params.somethingElse = Number(d.Params.somethingElse);
    }); 
};

Is there any way to rectify this?

cjlew
  • 15
  • 4

1 Answers1

1

Well, Params (without any prepending d.) is readily available within your inner function. This is called a [closure] https://stackoverflow.com/a/111111/444255), normal javascript featgure.

Just remove the d. before it.

d is an entirely different variable, basically the first-level sub-elemens of your data, that you are iterating about. (most likely object properties resp. array members).

Frank N
  • 9,625
  • 4
  • 80
  • 110
  • Thanks for your answer, but I have put a debugger inside the json request and Params is undefined, which is quite confusing to me. I have Params after the d to access specific data from the json data. – cjlew Nov 10 '17 at 16:40
  • remove the `d.`, then try the debugger again :) – the mean thing is, that variables only become a closure, if they do get used. Otherwise they are not around, in dev tools/debuggers. – Frank N Nov 10 '17 at 16:55
  • I got it working now thanks for the advice. Totally forgot that the variables wouldn't show if they weren't used. – cjlew Nov 10 '17 at 17:05