简体   繁体   中英

How can I convert .geojson file to a JavaScript object with nested arrays?

Newbie here, struggling to convert data into a complex structure. I have a very large .geojson file with the following simplified data format:

{
  "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
    ]
  }
},...

How can I convert it to the following format?

[{ 
       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,...]
}]

I've been able to get the first part of the object created with the inst_name: "Institution2", properties:{}, but I've been stuck attempting to build the rest of the object in the same function, as can be seen here in this plunker: https://plnkr.co/edit/340oshw78kEAOdFwZpH9?p=preview

Break down the tasks to smaller manageable chunks when you need to work with complex data.

->form the institutions array with simple properties.

->group the similar institutions into separate array.

->map the institution array to the grouped array based on value.

 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 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM