JavaScript D3 dynamic json key names dynamic json namespace,json dynamic name

When you want to make a graph in d3 and you read your input from a json file (for example) the code goes pretty much like this :

 d3.json("data/data.json", function(error, data) {
    data.forEach(function(d) {
        d.date = parseDate(d.date);
        d.close = +d.close;
    });

    // Scale the range of the data
    x.domain(d3.extent(data, function(d) { return d.date; }));
    y.domain([0, d3.max(data, function(d) { return d.close; })]);

    svg.append("path")      // Add the valueline path.
        .attr("d", valueline(data));

    svg.append("g")         // Add the X Axis
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .call(xAxis);

    svg.append("g")         // Add the Y Axis
        .attr("class", "y axis")
        .call(yAxis);
});

In this particular example I got from d3noob.org, there is a json file with objects that have 2 keys (date and close). What if I don't know the key names of the json file and I have to use a string variable? For example :

d3.json("data/data.json", function(error, data) {
        var var1 = "date";
        var var2 = "close";
        data.forEach(function(d) {
            d.var1 = parseDate(d.var1);
            d.var2 = +d.var2;
        });

I tried doing that, but the console gives me an error

d3.min.js:1 Error: attribute d: Expected number, "M0,NaNL106,NaNL212,…".

What am I doing wrong here?

Answer:1

Use the bracket [] notation to access an object property whose name is stored in a variable.

eg: d.var1 = parseDate(d.var1) becomes d[var1] = parseDate(d[var1]).

Answer:2

I just watched The Definitive Guide to Object-Oriented JavaScript. I will never learn how to do OOP in classical way. It mentioned the new spec (ES6). But as far as I know it is not well supported. Is ...

I just watched The Definitive Guide to Object-Oriented JavaScript. I will never learn how to do OOP in classical way. It mentioned the new spec (ES6). But as far as I know it is not well supported. Is ...

Considering the following code snippet, function outer(data1){ function inner(){ console.log(data1); } return inner; } in the following two function calls, first one works but ...

Considering the following code snippet, function outer(data1){ function inner(){ console.log(data1); } return inner; } in the following two function calls, first one works but ...

  1. closure working javascript
  2. javascript closure not working

Similar questions have been asked before, but I'm looking for the "opposite" solution than all the others ;-) I need a JavaScript event that fires when a user decides for an option in a select field. ...

Similar questions have been asked before, but I'm looking for the "opposite" solution than all the others ;-) I need a JavaScript event that fires when a user decides for an option in a select field. ...

  1. javascript make select readonly
  2. javascript make select required
  3. javascript make select option unselectable
  4. javascript make select option selected
  5. javascript make select disabled
  6. javascript make select option disabled
  7. javascript make select box readonly
  8. javascript make select option readonly
  9. javascript make select
  10. javascript make select empty
  11. javascript make select dropdown
  12. javascript make select list
  13. javascript make select element
  14. javascript make select option hidden

This is my CSS, in which, three keyframes are bound together to make a chain animation #anim-div{ -webkit-animation-name : mpbar-anim-page1, mpbar-anim-page2, mpbar-anim-page3; animation-...

This is my CSS, in which, three keyframes are bound together to make a chain animation #anim-div{ -webkit-animation-name : mpbar-anim-page1, mpbar-anim-page2, mpbar-anim-page3; animation-...