javascript - How to merge data from D3.js Topojson to defined set to set class -


so i'm using topojson functionality of d3.js create svg world map. i've managed figure out scale , far despite being relatively new d3. issue i'm having attempting merge or filter data topojson file sets have defined. i'm going leave out functions used projection. here's have far:

var svg = d3.select("#map svg"); var regions = [     {         region: 'low',         set: d3.set([ "usa", "can" ])     },     {         region: 'med',         set: d3.set([ "aus" ])     },     {         region: 'high',         set: d3.set([ "rus" ])     } ]; d3.json("site/js/topo.json", function(error, topology) {     // individual countries     svg.selectall("path")         .data(topojson.feature(topology, topology.objects.worldbyiso).features)         .enter()         .append("path")         .attr("data-iso", function(d){              return d.properties.iso;         })         .attr("d", path); }); 

i'm able return d.properties.iso value topojson file data attribute, i'd check iso property against 'regions' array instead return region (low, med, or high) if matches set.

ideally solution wouldn't need loop through 'regions' each time check can processed quickly. makes sense, i'm writing run out door overtime!

array.reduce

i think want array.reduce(). let collapse array single value. changed d3.sets simple arrays use contains() method quick glance @ d3 documentation looks sets have has() instead.

var regions = [{      region: 'low',      set: ["usa", "can"]    },    {      region: 'med',      set: ["aus"]    },    {      region: 'high',      set: ["rus"]    }  ];    let getregion = r => regions.reduce((a, c) => c.set.includes(r) ? c.region : a, null);    console.log(getregion('aus'));  console.log(getregion('usa'));  console.log(getregion('eur'));


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -