合并 json 并构建包含重复项的 csv

Merge json and build csv with duplicates

这是我拥有的一些地理json的示例:

var json = {
  features: [
    {
      properties: {
        osm_key: "amenity",
        extent: [
          151.214672,
          -33.8562966,
          151.2158814,
          -33.8574149
        ],
        street: "Lower Concourse",
        name: "Sydney Opera House",
        state: "New South Wales",
        osm_id: 4960757,
        osm_type: "W",
        postcode: "2061",
        osm_value: "theatre",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2152582491399,
          -33.85685575
        ]
      }
    },
    {
      properties: {
        osm_key: "tourism",
        extent: [
          151.214672,
          -33.8562966,
          151.2158814,
          -33.8574149
        ],
        street: "Lower Concourse",
        name: "Sydney Opera House",
        state: "New South Wales",
        osm_id: 4960757,
        osm_type: "W",
        postcode: "2061",
        osm_value: "attraction",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2152582491399,
          -33.85685575
        ]
      }
    },
    {
      properties: {
        osm_key: "highway",
        extent: [
          -95.6987584,
          29.9960185,
          -95.6984449,
          29.9907477
        ],
        name: "Opera House Row Drive",
        state: "Texas",
        osm_id: 261793234,
        osm_type: "W",
        postcode: "77433",
        osm_value: "residential",
        city: "Cypress",
        country: "United States of America"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          -95.698749,
          29.993634
        ]
      }
    },
    {
      properties: {
        osm_key: "tourism",
        street: "葆台路",
        name: "Sydney Opera House",
        state: "Beijing",
        osm_id: 3184358225,
        osm_type: "N",
        postcode: "100070",
        osm_value: "attraction",
        city: "Beijing",
        country: "China"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          116.2844542,
          39.8078321
        ]
      }
    },
    {
      properties: {
        osm_key: "amenity",
        street: "Macquarie Street",
        name: "Opera House Car Park",
        state: "New South Wales",
        osm_id: 2877110066,
        osm_type: "N",
        postcode: "2000",
        osm_value: "parking",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2135144,
          -33.8593646
        ]
      }
    }
  ],
  type: "FeatureCollection"
}

如您所见,Sydney Opera House 有 2 个条目 - 一个标记为 osm_value "theatre",另一个标记为 "attraction"

我无法控制被 return 编辑的数据,所以我需要一个 javascript 函数,我可以将 json 传递给它,它 return 一个相同格式的 geoJson 对象,但删除了重复项并将重复项的 osm_values 合并为 csv 格式,如:

var json = {
  features: [
    {
      properties: {
        osm_key: "amenity",
        extent: [
          151.214672,
          -33.8562966,
          151.2158814,
          -33.8574149
        ],
        street: "Lower Concourse",
        name: "Sydney Opera House",
        state: "New South Wales",
        osm_id: 4960757,
        osm_type: "W",
        postcode: "2061",
        osm_value: "theatre, attraction",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2152582491399,
          -33.85685575
        ]
      }
    },
    {
      properties: {
        osm_key: "highway",
        extent: [
          -95.6987584,
          29.9960185,
          -95.6984449,
          29.9907477
        ],
        name: "Opera House Row Drive",
        state: "Texas",
        osm_id: 261793234,
        osm_type: "W",
        postcode: "77433",
        osm_value: "residential",
        city: "Cypress",
        country: "United States of America"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          -95.698749,
          29.993634
        ]
      }
    },
    {
      properties: {
        osm_key: "tourism",
        street: "葆台路",
        name: "Sydney Opera House",
        state: "Beijing",
        osm_id: 3184358225,
        osm_type: "N",
        postcode: "100070",
        osm_value: "attraction",
        city: "Beijing",
        country: "China"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          116.2844542,
          39.8078321
        ]
      }
    },
    {
      properties: {
        osm_key: "amenity",
        street: "Macquarie Street",
        name: "Opera House Car Park",
        state: "New South Wales",
        osm_id: 2877110066,
        osm_type: "N",
        postcode: "2000",
        osm_value: "parking",
        country: "Australia"
      },
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [
          151.2135144,
          -33.8593646
        ]
      }
    }
  ],
  type: "FeatureCollection"
}

需要牢记以下几点:

我看到以下页面提到了类似的内容,但 adeneo 的回答似乎仅限于硬编码键: 而我需要它是动态的。

希望有人能提供帮助。

希望这个功能能解决问题。

    function mergeJson(json) {
    //invalid object
    if (!json || !json.features) {
        return;
    }
    var features = json.features;
    var added = {};
    for (var i = 0, l = features.length; i < l; i++) {
        var o = features[i].properties;
        if (!o) {
            continue;
        }
        if (added.hasOwnProperty(o.name)) {
            if (added[o.name].osm_value.indexOf(o.osm_value) === -1) {
                added[o.name].osm_value = added[o.name].osm_value + "," + o.osm_value;
            }
            //remove the object and subtract and size
            json.features.splice(i--, 1), l--;
        } else {
            added[o.name] = o;
        }
    }
}

mergeJson(json);

输出

 {
    "features": [{
        "properties": {
            "osm_key": "amenity",
            "extent": [151.214672, -33.8562966, 151.2158814, -33.8574149],
            "street": "Lower Concourse",
            "name": "Sydney Opera House",
            "state": "New South Wales",
            "osm_id": 4960757,
            "osm_type": "W",
            "postcode": "2061",
            "osm_value": "theatre,attraction",
            "country": "Australia"
        },
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [151.2152582491399, -33.85685575]
        }
    }, {
        "properties": {
            "osm_key": "highway",
            "extent": [-95.6987584, 29.9960185, -95.6984449, 29.9907477],
            "name": "Opera House Row Drive",
            "state": "Texas",
            "osm_id": 261793234,
            "osm_type": "W",
            "postcode": "77433",
            "osm_value": "residential",
            "city": "Cypress",
            "country": "United States of America"
        },
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [-95.698749, 29.993634]
        }
    }, {
        "properties": {
            "osm_key": "amenity",
            "street": "Macquarie Street",
            "name": "Opera House Car Park",
            "state": "New South Wales",
            "osm_id": 2877110066,
            "osm_type": "N",
            "postcode": "2000",
            "osm_value": "parking",
            "country": "Australia"
        },
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [151.2135144, -33.8593646]
        }
    }],
    "type": "FeatureCollection"
};