Javascript 如何将json文件中的键值组合到geojson映射文件中?
我正在尝试将GeoJSON映射文件与JSON文件中的键值组合起来,以用于choropleth映射 以下是文件的外观:Javascript 如何将json文件中的键值组合到geojson映射文件中?,javascript,json,leaflet,geojson,Javascript,Json,Leaflet,Geojson,我正在尝试将GeoJSON映射文件与JSON文件中的键值组合起来,以用于choropleth映射 以下是文件的外观: { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "STATE": "06", "ZIPCODE": "94601",
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94601",
"crime": 118,
"income": 28097
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
},
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94501",
"crime": 172,
"income": 9456
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
}
]
}
data1.json
data2.json
下面是我希望组合对象的外观:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94601",
"crime": 118,
"income": 28097
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
},
{
"type": "Feature",
"properties": {
"STATE": "06",
"ZIPCODE": "94501",
"crime": 172,
"income": 9456
},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
}
]
}
目前,我的代码如下所示:
d3.json("data1.json", function (geoData) {
d3.json("data2.json", function (zipdata) {
var geoFeatures = geoData.features;
for (var i = 0; i < geoFeatures.length; i++) {
Object.keys(zipdata).forEach(function (key) {
if (geoFeatures[i].properties.ZIPCODE == key) {
var combined = Object.assign({}, geoFeatures[i], zipdata[key]);
console.log(combined);
}
})
}
})
})
这使我接近我想要的,但我希望保留data1.json中显示的GeoJSON映射格式。您可以在features数组上循环并检查data2上的zipcode值(如果有),将其添加到相应元素的属性中 设obj={type:FeatureCollection,features:[{type:Feature,properties:{STATE:06,ZIPCODE:94601},geometry:{type:Polygon,坐标:[……]},{type:Feature,properties:{STATE:06,ZIPCODE:94501},geometry:{type:Polygon,坐标:[……]} 让数据2={94501:{犯罪:172,收入:9456,},94601:{犯罪:118,收入:28097,} obj.features.forEachval=>{ 设{properties}=val 让newProps=data2[properties.ZIPCODE] val.properties={…properties,…newProps} }
logobj您可以在features数组上循环并检查data2上的zipcode值(如果有),并将其添加到相应元素的属性中 设obj={type:FeatureCollection,features:[{type:Feature,properties:{STATE:06,ZIPCODE:94601},geometry:{type:Polygon,坐标:[……]},{type:Feature,properties:{STATE:06,ZIPCODE:94501},geometry:{type:Polygon,坐标:[……]} 让数据2={94501:{犯罪:172,收入:9456,},94601:{犯罪:118,收入:28097,} obj.features.forEachval=>{ 设{properties}=val 让newProps=data2[properties.ZIPCODE] val.properties={…properties,…newProps} } console.logobj试试这个: 让data1={type:FeatureCollection,features:[{type:Feature,properties:{STATE:06,ZIPCODE:94601},geometry:{type:Polygon,座标:[……]},{type:Feature,properties:{STATE:06,ZIPCODE:94501},geometry:{type:Polygon,座标:[……]} 让数据2={94501:{犯罪:172,收入:9456,},94601:{犯罪:118,收入:28097,} data1.features.mapres=>Object.assignres{ 特性:{ …物属性, …数据2[res.properties.ZIPCODE] } } console.logdata1试试这个: 让data1={type:FeatureCollection,features:[{type:Feature,properties:{STATE:06,ZIPCODE:94601},geometry:{type:Polygon,座标:[……]},{type:Feature,properties:{STATE:06,ZIPCODE:94501},geometry:{type:Polygon,座标:[……]} 让数据2={94501:{犯罪:172,收入:9456,},94601:{犯罪:118,收入:28097,} data1.features.mapres=>Object.assignres{ 特性:{ …物属性, …数据2[res.properties.ZIPCODE] } } console.logdata1
d3.json("data1.json", function (geoData) {
d3.json("data2.json", function (zipdata) {
var geoFeatures = geoData.features;
for (var i = 0; i < geoFeatures.length; i++) {
Object.keys(zipdata).forEach(function (key) {
if (geoFeatures[i].properties.ZIPCODE == key) {
var combined = Object.assign({}, geoFeatures[i], zipdata[key]);
console.log(combined);
}
})
}
})
})