I am passing in a full state name through a function called findAbbreviation(fullName) Im using a csv file of fullname, abbreviation pairs to return the value of the abbreviation that matches the fullname. What is the best way to do this, given that I want the read through of the csv to be synchronous?

d3.csv("candy.csv", function(csv) {

//reformat the locations to have a consistent format
//every entry in q5 is changed to an abbreviation of 2 letters
//the abbreviation lists are in the folder named such
var updatedAbbr = csv.map(function(d) {

    //check to see the entry has a value
    if (d.Q5_STATE != null) {

        //the country is the united States
        if (d.Q4_COUNTRY == "United States") {

            //if the state value is longer than two characters, set it to UU
            if (d.Q5_STATE.length > 2) {
                d.Q5_STATE = findAbbreviation(d.Q5_STATE);  
            }

            //pass through and check that the new value is not null
            if (d.Q5_STATE != null) {
                console.log(d.Q5_STATE);    
            }


        }
    }
});

});

function findAbbreviation(fullName) {

}

I'd like to pass in the value Alabama and get AL. Or California -> CA.

1 Answers

0
Quentin On

d3.csv is asynchronous, you can't do anything about that.

You could use XMLHttpRequest to make a synchronous request (Danger: This feature is deprecated) and then feed the result through d3.csv.parse.

I recommend embracing asynchronous programming instead.