Creating dictionary with map & filter?

后端 未结 1 1696
孤城傲影
孤城傲影 2021-01-26 20:31

I have the following map and filter functions in order to get my csv file data with their column names as keys.

d3.csv(\"Sales Export Friendly 3-19-17.         


        
相关标签:
1条回答
  • 2021-01-26 20:49

    When you load a CSV file, d3.csv creates a handy array property called columns.

    According to the API:

    The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).

    Thus, since your callback parameter is named data, you can get the columns' names simply using:

    data.columns
    

    Or assigning it to a variable:

    var myColumns = data.columns
    

    Here is a demo with a real CSV file from this Bostock's bl.ocks:

    d3.csv("https://gist.githubusercontent.com/mbostock/3887051/raw/805adad40306cedf1a513c252ffffd95e7c981885a/data.csv", function(data){
      console.log(data.columns);
    });
    <script src="https://d3js.org/d3.v4.min.js"></script>

    PS: this answer refers to D3 v4, which should be the default version for answers, unless stated otherwise in the question.

    0 讨论(0)
提交回复
热议问题