-4

I have been trying to read multiple csv file for d3 library following other stack overflow. All csv data will be stored in array results. But checking at results by chrome developer tool, data is stored to result weird way as shown in below image. I can see that data is stored to results array, but I cannot specify elements stored in the array. Can anyone tell me how I can specify elements in this array case? or tell me how to avoid storing data to array this way?

I can see that data is stored in array, but if I try to get element from array ,they return "undefined":

function multicsv(files){
    var results=[];
    var categories=[];
    //var category=[];
    var filesLength = (files || []).length;
    //console.log(filesLength);
    var count=0;
    var counter=function(){
        return count++
    }
    for (var i=0 ;i<filesLength;i++){
        d3.csv(files[i], function(data){
            ix=counter();
            data,category=get_category(data,ix);
            //console.log(data);
            arr=fitstruct(category,data);
            //console.log(arr);
            //results.push(arr);
            results[ix]=arr
            //categories.push(category)
            categories[ix]=category
        });
    }

    console.log(results);
    console.log(results[0]);
};

function get_category(data,ix){
    var category=[]
    data.forEach(function(d){
        if(d.Subcategory=="General"){
            d.category=d[""]
            category.push(d[""]);
        }else if(d.Subcategory!="General"){
            d.category=d.Subcategory;
            category.push(d.Subcategory);
        };
        d.electric=+d["Electricity [kWh]"];
        d.version=ix;
    })
    return data,category
};

function fitstruct(list,data){
        var electricity=[];
        var total=0
        for (i=0;i<list.length;i++){
            //electricity.push(category[i],data[i]["electric"])
            electricity[list[i]]=data[i]["electric"]/1000000
            total+=data[i]["electric"]
        }
        electricity["version"]=data[0]["version"];
        electricity["total"]=total/1000000
        return electricity
      };
Community
  • 1
  • 1
Katsuya Obara
  • 903
  • 3
  • 14
  • 30
  • Downvoters: Instead of just voting down, leave this newcomer with the reasons why you are voting it down. @Katsuya: Please have a look at [mcve] – mplungjan Nov 14 '16 at 08:39
  • In the image I can only see that `console.log(categories);` returns `[]`, which means the `categories` array is empty. When an array is empty you will receive an `undefined` when you try to access an element by `categories[0]`. – gus27 Nov 14 '16 at 08:50
  • In your `multicsv` function there are semicolons missing at the end of two lines which could have unwanted side effects. Always use semicolons :) Beside that a `var` is missing before declaration of `ix` variable. – gus27 Nov 14 '16 at 08:53
  • In `get_category` you try to return multiple values from a function. AFAIK this is not possible. Try something like [this](http://stackoverflow.com/questions/2917175/return-multiple-values-in-javascript) instead. – gus27 Nov 14 '16 at 08:57
  • Even though console.log(categories) returns [], I can see 2 array is contained in the variable.( Pushing the triangle button next to [], contents is shown beneath) Does anyone know how to access these array? – Katsuya Obara Nov 15 '16 at 05:37

1 Answers1

0

This issue is either related to D3 specifically, or more likely a JSON issue.

You can help yourself by viewing the JSON more clearly with tools like jsonlint.com. To do so:

  1. add this line to your code

    console.log(JSON.stringify(my_json_variable))

  2. Press F12 on the browser and select console.

  3. Run the code
  4. Copy & paste the JSON string from the console into json viewer and view.

Many times errors in code are purely programmer assumption errors. Visualising the JSON in this way can help set aside assumptions.

Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67