How to select unique values in d3.js from data

后端 未结 2 1847
醉梦人生
醉梦人生 2021-02-14 06:49

I am using the following code to populate my Combobox using d3.js

d3.csv(\"Results_New.txt\", function(data) {
dataset=data;
d3.select(\"#road\").selectAll(\"opt         


        
2条回答
  •  时光取名叫无心
    2021-02-14 06:53

    d3.map is deprecated - the d3 devs now recommend simply using JavaScript's built-in Array.map. (In fact the entire d3-collection module is now deprecated, with similar rationale for d3.set.)

    Here's a concise and (I think) elegant way to do this in ES6, using Array.map, Set, and the spread operator ...:

    let options = [...new Set(data.map(d => d.roadname))]; 
    // optionally add .sort() to the end of that line to sort the unique values
    // alphabetically rather than by insertion order
    
    d3.select('#road')
      .selectAll('option')
        .data(options)
      .enter()
        .append('option')
        .text(d => d)
        .attr('value', d => d);
    

提交回复
热议问题