如何将 .geojson 文件转换为具有嵌套数组的 JavaScript 对象?
How can I convert .geojson file to a JavaScript object with nested arrays?
这里是新手,正在努力将数据转换为复杂的结构。我有一个非常大的 .geojson 文件,具有以下简化数据格式:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"group_name": "AAA",
"inst_name": "Institution1",
"holdings": "100,000+",
"all_titles": "500,000+",
"region": "New York"
},
"geometry": {
"type": "Point",
"coordinates": [
-86.1762,
39.7742
]
}
},
{
"type": "Feature",
"properties": {
"group_name": "AAA",
"inst_name_long": "Institution2",
"holdings": "100,000+",
"all_titles": "500,000+",
"region": "New York"
},
"geometry": {
"type": "Point",
"coordinates": [
-87.4106,
39.4705
]
}
},...
如何转换成下面的格式?
[{
inst_name: "Institution1",
properties: {group_name: "AAA", holdings: "<100,000", all_titles: "250,000+", region: "Indiana"},
group_name: "AAA",
group_members: ["Institute1","Institute2,...],
region_name: "New York",
region_members: ["Institute1","Institute2,...],
collection_name: "100,000+",
collection_members: ["Institute1","Institute2,...],
shared_name: "500,000+",
shared_members: ["Institute1","Institute2,...]
},{
inst_name: "Institution2",
properties: {group_name: "AAA", holdings: "<100,000", all_titles: "250,000+", region: "Indiana"},
group_name: "AAA",
group_members: ["Institute1","Institute2,...],
region_name: "New York",
region_members: ["Institute1","Institute2,...],
collection_name: "100,000+",
collection_members: ["Institute1","Institute2,...],
shared_name: "500,000+",
shared_members: ["Institute1","Institute2,...]
}]
我已经能够获取使用创建的对象的第一部分
inst_name: "Institution2",
properties:{},
但我一直试图在同一个函数中构建对象的其余部分,正如在这个 plunker 中看到的那样:https://plnkr.co/edit/340oshw78kEAOdFwZpH9?p=preview
当您需要处理复杂数据时,将任务分解为更小的可管理块。
->形成具有简单属性的机构数组。
->将类似的机构分组到单独的数组中。
->将机构数组映射到基于值的分组数组。
fetch('./geoJsondata.json')
.then((res)=>res.json())
.then(handleResponse)
.catch((err)=>console.log(err))
function handleResponse(res) {
let propsToGroupBy = ['region', 'all_titles', 'holdings', 'group_name']
console.log(flattenJSON(res, propsToGroupBy));
return flattenJSON(res, propsToGroupBy)
}
function flattenJSON(geoJSON, propsToGroupBy) {
let { institutions, groups } =
geoJSON.features.reduce(
reduceGroupsAndGetLists,
{ institutions: [], groups: {}, groupByProps: propsToGroupBy });
let flattendJSON = institutions.map(toInstList(groups));
function reduceGroupsAndGetLists (acc, {properties}) {
let {
inst_name_long: inst_name,
group_name,
region,
holdings,
all_titles
} = properties;
acc.institutions.push({
properties,
inst_name,
group_name,
region,
holdings,
all_titles
});
acc.groupByProps.map((prop) => {
if (
(acc.groups[prop] || (acc.groups[prop] = {})))
&&
(
acc.groups[prop][properties[prop]] ||
(acc.groups[prop][properties[prop]] = [])
))
acc.groups[prop][properties[prop]].push(inst_name);
});
return acc;
}
function toInstList (groups) {
return (institution) => {
propsToGroupBy.map((prop) => {
institution[`${prop}_members`] = groups[prop][institution[prop]];
});
return institution;
}
}
return flattendJSON
}
这里是新手,正在努力将数据转换为复杂的结构。我有一个非常大的 .geojson 文件,具有以下简化数据格式:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"group_name": "AAA",
"inst_name": "Institution1",
"holdings": "100,000+",
"all_titles": "500,000+",
"region": "New York"
},
"geometry": {
"type": "Point",
"coordinates": [
-86.1762,
39.7742
]
}
},
{
"type": "Feature",
"properties": {
"group_name": "AAA",
"inst_name_long": "Institution2",
"holdings": "100,000+",
"all_titles": "500,000+",
"region": "New York"
},
"geometry": {
"type": "Point",
"coordinates": [
-87.4106,
39.4705
]
}
},...
如何转换成下面的格式?
[{
inst_name: "Institution1",
properties: {group_name: "AAA", holdings: "<100,000", all_titles: "250,000+", region: "Indiana"},
group_name: "AAA",
group_members: ["Institute1","Institute2,...],
region_name: "New York",
region_members: ["Institute1","Institute2,...],
collection_name: "100,000+",
collection_members: ["Institute1","Institute2,...],
shared_name: "500,000+",
shared_members: ["Institute1","Institute2,...]
},{
inst_name: "Institution2",
properties: {group_name: "AAA", holdings: "<100,000", all_titles: "250,000+", region: "Indiana"},
group_name: "AAA",
group_members: ["Institute1","Institute2,...],
region_name: "New York",
region_members: ["Institute1","Institute2,...],
collection_name: "100,000+",
collection_members: ["Institute1","Institute2,...],
shared_name: "500,000+",
shared_members: ["Institute1","Institute2,...]
}]
我已经能够获取使用创建的对象的第一部分
inst_name: "Institution2",
properties:{},
但我一直试图在同一个函数中构建对象的其余部分,正如在这个 plunker 中看到的那样:https://plnkr.co/edit/340oshw78kEAOdFwZpH9?p=preview
当您需要处理复杂数据时,将任务分解为更小的可管理块。
->形成具有简单属性的机构数组。
->将类似的机构分组到单独的数组中。
->将机构数组映射到基于值的分组数组。
fetch('./geoJsondata.json')
.then((res)=>res.json())
.then(handleResponse)
.catch((err)=>console.log(err))
function handleResponse(res) {
let propsToGroupBy = ['region', 'all_titles', 'holdings', 'group_name']
console.log(flattenJSON(res, propsToGroupBy));
return flattenJSON(res, propsToGroupBy)
}
function flattenJSON(geoJSON, propsToGroupBy) {
let { institutions, groups } =
geoJSON.features.reduce(
reduceGroupsAndGetLists,
{ institutions: [], groups: {}, groupByProps: propsToGroupBy });
let flattendJSON = institutions.map(toInstList(groups));
function reduceGroupsAndGetLists (acc, {properties}) {
let {
inst_name_long: inst_name,
group_name,
region,
holdings,
all_titles
} = properties;
acc.institutions.push({
properties,
inst_name,
group_name,
region,
holdings,
all_titles
});
acc.groupByProps.map((prop) => {
if (
(acc.groups[prop] || (acc.groups[prop] = {})))
&&
(
acc.groups[prop][properties[prop]] ||
(acc.groups[prop][properties[prop]] = [])
))
acc.groups[prop][properties[prop]].push(inst_name);
});
return acc;
}
function toInstList (groups) {
return (institution) => {
propsToGroupBy.map((prop) => {
institution[`${prop}_members`] = groups[prop][institution[prop]];
});
return institution;
}
}
return flattendJSON
}