导入时从 json 创建键值对
Creating key value pairs from json on import
我是 jquery 的新手 - 我有一个有效的 geojson 文件,我想访问它的 features
并将其转换为键值对的对象。我的 objective 是只使用 properties.cat
作为键, properties.name
作为值(所有其他数据都可以忽略)。这是一个示例:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "cat": "A", "name": "Aberdeen"}, "geometry": { "type": "Point", "coordinates": [ 16.37208, 48.20849 ] } },
{ "type": "Feature", "properties": { "cat": "B", "name": "Berlin"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "C", "name": "Copenhagen"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "D", "name": "Dublin" }, "geometry": { "type": "Point", "coordinates": [ 12.56553, 55.67594 ] } },
{ "type": "Feature", "properties": { "cat": "E", "name": "Edinburgh"}, "geometry": { "type": "Point", "coordinates": [ -3.7037902, 40.4167754 ] } }
]
}
$.getJSON("sample.geojson", function(json) {
console.log(json.features[0].properties.cat);
});
如下所述,features
是一个数组。
如何直接从 json 中的每个特征 属性 创建一个键值对对象,以便获得以下输出:
{A : Aberdeen, B: Berlin, C: Copenhagen, D: Dublin, E: Edinburgh}
$.getJson
方法的回调函数已经自动解析了响应。
此外,features 对象是一个数组。使用这个:
console.log(json.features[0].properties.cat);
要制作键值对,您可以使用 reduce
方法,该方法接受 callback
提供的应用于每个项目的方法。
var json= {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "cat": "A", "name": "Aberdeen"}, "geometry": { "type": "Point", "coordinates": [ 16.37208, 48.20849 ] } },
{ "type": "Feature", "properties": { "cat": "B", "name": "Berlin"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "C", "name": "Copenhagen"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "D", "name": "Dublin" }, "geometry": { "type": "Point", "coordinates": [ 12.56553, 55.67594 ] } },
{ "type": "Feature", "properties": { "cat": "E", "name": "Edinburgh"}, "geometry": { "type": "Point", "coordinates": [ -3.7037902, 40.4167754 ] } }
]
}
var obj=json.features.reduce(function(obj,item){
obj[item.properties.cat]=item.properties.name;
return obj;
},{});
console.log(obj);
我是 jquery 的新手 - 我有一个有效的 geojson 文件,我想访问它的 features
并将其转换为键值对的对象。我的 objective 是只使用 properties.cat
作为键, properties.name
作为值(所有其他数据都可以忽略)。这是一个示例:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "cat": "A", "name": "Aberdeen"}, "geometry": { "type": "Point", "coordinates": [ 16.37208, 48.20849 ] } },
{ "type": "Feature", "properties": { "cat": "B", "name": "Berlin"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "C", "name": "Copenhagen"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "D", "name": "Dublin" }, "geometry": { "type": "Point", "coordinates": [ 12.56553, 55.67594 ] } },
{ "type": "Feature", "properties": { "cat": "E", "name": "Edinburgh"}, "geometry": { "type": "Point", "coordinates": [ -3.7037902, 40.4167754 ] } }
]
}
$.getJSON("sample.geojson", function(json) {
console.log(json.features[0].properties.cat);
});
如下所述,features
是一个数组。
如何直接从 json 中的每个特征 属性 创建一个键值对对象,以便获得以下输出:
{A : Aberdeen, B: Berlin, C: Copenhagen, D: Dublin, E: Edinburgh}
$.getJson
方法的回调函数已经自动解析了响应。
此外,features 对象是一个数组。使用这个:
console.log(json.features[0].properties.cat);
要制作键值对,您可以使用 reduce
方法,该方法接受 callback
提供的应用于每个项目的方法。
var json= {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { "cat": "A", "name": "Aberdeen"}, "geometry": { "type": "Point", "coordinates": [ 16.37208, 48.20849 ] } },
{ "type": "Feature", "properties": { "cat": "B", "name": "Berlin"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "C", "name": "Copenhagen"}, "geometry": { "type": "Point", "coordinates": [ 4.3517103, 50.8503396 ] } },
{ "type": "Feature", "properties": { "cat": "D", "name": "Dublin" }, "geometry": { "type": "Point", "coordinates": [ 12.56553, 55.67594 ] } },
{ "type": "Feature", "properties": { "cat": "E", "name": "Edinburgh"}, "geometry": { "type": "Point", "coordinates": [ -3.7037902, 40.4167754 ] } }
]
}
var obj=json.features.reduce(function(obj,item){
obj[item.properties.cat]=item.properties.name;
return obj;
},{});
console.log(obj);