D3 Topojson 添加名称 属性 到 FeatureCollection

D3 Topojson Adding Name Property to FeatureCollection

我通过像这样过滤所需的 FIPS id 硬编码了一个合并的多边形区域:

var set1 = d3.set([
  48111, 48421, 48195, 48357, 48295, 48205, 48341, 48233, 48393, 48211,
  48359, 48375, 48065, 48179, 48483, 48485, 48077, 48337, 48237, 48009,
  48503, 48023, 48269, 48125, 48107, 48303, 48219, 48079, 48501, 48445,
  48117, 48381, 48011, 48129, 48087, 48369, 48069, 48437, 48045, 48191,
  48075, 48017, 48279, 48189, 48153, 48345, 48101, 48155, 48197, 48487,
  48305, 48169
]);

var region1 = {type: "FeatureCollection", features: counties.filter(function(d) {return set1.has(d.id); })};

我的问题是:如果我想为这个区域添加一个名称属性,我该如何进行?我想用区域变量声明一些东西,可能是这样的:

var region1 = {type: "FeatureCollection", properties: 'Name': 'My Region', features: counties.filter(function(d) {return set1.has(d.id); })};

你有什么...

properties: 'Name': 'My Region'

... 不是有效的 JavaScript 对象。应该是:

var region1 = {
    type: "FeatureCollection",
    name: 'My Region',
    features: counties.filter(function(d) {
        return set1.has(d.id);
    })
};

或者,如果您希望它们在 properties 属性 内:

var region1 = {
    type: "FeatureCollection",
    properties: {
        name: 'My Region',
        features: counties.filter(function(d) {
            return set1.has(d.id);

        })
    }
};

此外,JavaScript 中的惯例是对 属性 名称使用小写字母,因此我建议您使用 name,而不是 Name

这是一个 Bostock 的 bl.ocks 和你的对象,看看控制台:https://bl.ocks.org/anonymous/3fb65e6b6204e5731afc4152a35a0179/0d579d98e599cef73284c5e89b92e7fe94be42ab