[英]how to retrieve specific keys / values from json object and add into another json object in jQuery
[英]How to add specific values from 2 JSON objects, to a new JSON object
我已經提取了一個國家出現在一個對象中的次數(顯示在底部)。 我的問題是我需要地理編碼信息及其國家名稱+顯示的次數,因此我可以在地圖上的實際國家/地區上繪制實際結果。
因此,從下面的對象中,mapToProp函數產生的對象將是
Germany:3,
United Kingdom: 1
但是我需要類似下面的內容。 因為這是mapbox的格式,所以它似乎期望geojson對象成為。
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"name":'Germany',
"amount": 3
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-77.034084142948,
38.909671288923
]
},
"properties": {
"name":'United Kingdom',
"amount": 4
}
}
]
}
獲取國家名稱+它們出現的時間
function mapToProp(data, prop) {
return data
.reduce((res, item) => Object
.assign(res, {
[item[prop]]: 1 + (res[item[prop]] || 0)
}), Object.create(null))
;
}
從地址解析器返回的結果(這是在其中顯示某些特定標題的國家/地區)
var data = [
{
bbox: [
5.866003, 47.270461, 15.041428, 55.0845576
],
center: [
10, 51
],
geometry: {
coordinates:[
10, 51
],
type: "Point"
}
}, {
id: "country.10743216036480410",
place_name: "Germany",
place_type: ["country"],
properties: {
short_code: "de",
wikidata: "Q183"
},
relevance: 1,
text: "Germany",
type: "Feature"
}, {
bbox: [
5.866003, 47.270461, 15.041428, 55.0845576
],
center: [
10, 51
],
geometry: {
type: "Point",
coordinates:[
10, 51
]
},
id: "country.10743216036480410",
place_name: "Germany",
place_type: ["country"],
properties: {
short_code: "de",
wikidata: "Q183"
},
relevance: 1,
text: "Germany",
type: "Feature"
}, {
bbox: [
5.866003, 47.270461, 15.041428, 55.0845576
],
center: [
10, 51
],
geometry: {
type: "Point",
coordinates:[
10, 51
]
},
id: "country.10743216036480410",
place_name: "Germany",
place_type: ["country"],
properties: {
short_code: "de",
wikidata: "Q183"
},
relevance: 1,
text: "Germany",
type: "Feature"
}, {
bbox: [
-8.718659, 49.802665, 1.867399, 60.945453
],
center: [
-2, 54
],
geometry: {
type: "Point",
coordinates:[
10, 51
],
},
id: "country.8605848117814600",
place_name: "United Kingdom",
place_type: ["country"],
properties: {
short_code: "gb",
wikidata: "Q145"
},
relevance: 1,
text: "United Kingdom",
type: "Feature"
}
]
編輯:
根據更新所需的結果(按type
分組,然后按place_name
分組),您可以執行以下兩種方法...
快速又臟(更易碎)
data
數組僅包含=== Feature
type
對象,因此您只需添加該屬性並將其嵌套在features
數組中即可返回... function groupByProp(data, prop) {
let objsByPlaceName = data.reduce((res, item) => {
if (!item[prop])
return res;
let existing = res[item[prop]],
amount = existing && existing.amount
? existing.amount + 1
: 1,
newObj = (() => {
if (existing && existing.geometry)
return {amount, geometry: existing.geometry};
if (item.geometry)
return {amount, geometry: item.geometry};
return {amount};
})();
return Object.assign(res, {
[item[prop]]: newObj
})
}, {})
return {
"type": "FeatureCollection",
"features": Object.keys(objsByPlaceName).map(key=> {
let obj = objsByPlaceName[key];
return {
type: "Feature",
geometry: obj.geometry,
properties: {
name: key,
amount: obj.amount
}
}
})
}
}
有更有效的方法來處理此問題(N1 vs N ^ 2),但這將需要大量處理屬性名稱和操縱深度嵌套的值。 我建議您保留初始的groupByProp()
邏輯,然后將數據展平為所需的結構。
type
不同的元素分組,則必須首先按該屬性分組。 給定數據結構,這實際上需要花費大量的工作,因此讓我知道您是否真的需要解決方案,然后再將其組合在一起。 初始答案:
function groupByProp(data, prop) {
return data
.reduce((res, item) => {
if (!item[prop]) return res;
let existing = res[item[prop]],
amount = existing && existing.amount ? existing.amount + 1 : 1,
newObj = (()=> {
if (existing && existing.geometry) return {amount, geometry: existing.geometry};
if (item.geometry) return {amount, geometry: item.geometry};
return {amount};
})();
return Object.assign(res, { [item[prop]]: newObj } )
},{});
}
groupByProp(data, 'place_name')
除了您提供的data
數組中的第一個元素沒有place_name
屬性外,這可能與您要查找的內容很接近,因此,如果您不想以其他方式處理,則必須更改此行...
if (!item[prop]) return res;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.