gpt4 book ai didi

javascript - 合并 json 并构建具有重复项的 csv

转载 作者:行者123 更新时间:2023-12-03 10:19:23 25 4
gpt4 key购买 nike

这是我拥有的一些 geojson 的示例:

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"
}

如您所见,悉尼歌剧院 有 2 个条目 - 一个标记为 osm_value “theatre”,另一个标记为 “景点”

我无法控制返回的数据,所以我需要一个 javascript 函数,我可以将 json 传递给它,它以相同的格式返回一个 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"
}

需要牢记以下几点:

  • 可能有也可能没有任何重复项需要过滤掉
  • 返回的 geoJson 必须保持相同的顺序,但删除重复项
  • 重复项不能是 features 数组中的相邻项
  • json 的结构应该或多或少保持相同,但 geoJson 中的键不应该被硬编码,因为额外的子键(例如 坐标street ) 可以随时添加
  • 通过具有相同的 properties.osm_idproperties.osm_type 来识别重复项
  • 页面正在使用jquery

我已经看到以下页面提到了类似的内容,但 adeneo 的答案似乎仅限于硬编码 key :https://stackoverflow.com/a/19118236/1116573而我需要它是动态的。

希望有人能帮忙。

最佳答案

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

    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"
};

关于javascript - 合并 json 并构建具有重复项的 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com