-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
      };
5
  • Downvoters: Instead of just voting down, leave this newcomer with the reasons why you are voting it down. @Katsuya: Please have a look at minimal reproducible example Commented Nov 14, 2016 at 8: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]. Commented Nov 14, 2016 at 8: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. Commented Nov 14, 2016 at 8:53
  • In get_category you try to return multiple values from a function. AFAIK this is not possible. Try something like this instead. Commented Nov 14, 2016 at 8: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? Commented Nov 15, 2016 at 5:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.