我有以下JSON Feed:
var data = {
"feeds": {
"regions": [
{
"name": "Lichtenberg",
"id": "01408.b",
"suburbs": [
{ "name": "Fennpfuhl", "views": 76400 },
{ "name": "Lichtenberg", "views": 87895 },
{ "name": "Rummelsberg", "views": 10239 }
]
},
{
"name": "Mitte",
"id": "03442.f",
"suburbs": [
{ "name": "Tiergarten", "views": 82695 },
{ "name": "Mitte", "views": 67234 },
{ "name": "Hansaviertel", "views": 10848 },
{ "name": "Moabit", "views": 67500 }
]
},
{
"name": "Friedrichshain-Kreuzberg",
"id": "01991.o",
"suburbs": [
{ "name": "Friedrichshain", "views": "98494" },
{ "name": "Kreuzberg", "views": "27800" }
]
},
{
"name": "Templehof-Schöneberg",
"id": "01778.k",
"suburbs": [
{ "name": "Friedenau", "views": 76595 },
{ "name": "Schöneberg", "views": 20731 },
{ "name": "Templehof", "views": 58000 },
{ "name": "Mariendorf", "views": 32300 }
]
},
{
"name": "Pankow",
"id": "02761.q",
"suburbs": [
{ "name": "Wießensee", "views": 81294 },
{ "name": "Prenzlauer Berg", "views": 76470 },
{ "name": "Pankow", "views": 90210 }
]
}
],
"branding": [
{
"municipality_id": "01408.b",
"brand_color": "#f9cd90"
},{
"municipality_id": "03442.f",
"brand_color": "#F28123"
},{
"municipality_id": "01991.o",
"brand_color": "#D34E24"
},{
"municipality_id": "01778.k",
"brand_color": "#563F1B"
},{
"municipality_id": "02761.q",
"brand_color": "#38726C"
}
],
"customer": {
"name": "Viktoria Tiedemann",
"date_of_birth": "1981-09-19",
"address": {
"street": "Schönfließer Str 9",
"suburb": "Prenzlauer Berg",
"postcode": "10439"
}
}
}
};
本质上,我想做的是创建一个包含3个项目的数组:
data.feeds.regions.name
data.feeds.regions.id
的图表颜色,然后将其用作data.branding
的查找关键字以获取该区域的brand_color
。 我从上一个SO问题中得到了第1部分和第2部分的答案:
var viewsPerRegion = data.feeds.regions.map(({ name, suburbs }) => ({
label: name,
total: suburbs.reduce((a, { views }) => a + Number(views), 0)
}));
到目前为止,我试图获得第三名的尝试如下:
var viewsPerRegionStyled = data.feeds.regions.map(({ name, id, suburbs }) => ({
label: name,
total: suburbs.reduce((a, { views }) => a + Number(views), 0),
color: if (data.feeds.region.id == data.branding.municipality_id)
{
data.branding.brand_color}
}));
我敢肯定,我对这一切完全迷失了-任何帮助都将不胜感激。
答案 0 :(得分:1)
您需要在func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 25.0, height: 25.0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
数组上调用find
,以找到具有匹配的branding
的元素,然后提取找到的municipality_id
属性:
brand_color
另一种选择是将var data={"feeds":{"regions":[{"name":"Lichtenberg","id":"01408.b","suburbs":[{"name":"Fennpfuhl","views":76400},{"name":"Lichtenberg","views":87895},{"name":"Rummelsberg","views":10239}]},{"name":"Mitte","id":"03442.f","suburbs":[{"name":"Tiergarten","views":82695},{"name":"Mitte","views":67234},{"name":"Hansaviertel","views":10848},{"name":"Moabit","views":67500}]},{"name":"Friedrichshain-Kreuzberg","id":"01991.o","suburbs":[{"name":"Friedrichshain","views":"98494"},{"name":"Kreuzberg","views":"27800"}]},{"name":"Templehof-Schöneberg","id":"01778.k","suburbs":[{"name":"Friedenau","views":76595},{"name":"Schöneberg","views":20731},{"name":"Templehof","views":58000},{"name":"Mariendorf","views":32300}]},{"name":"Pankow","id":"02761.q","suburbs":[{"name":"Wießensee","views":81294},{"name":"Prenzlauer Berg","views":76470},{"name":"Pankow","views":90210}]}],"branding":[{"municipality_id":"01408.b","brand_color":"#f9cd90"},{"municipality_id":"03442.f","brand_color":"#F28123"},{"municipality_id":"01991.o","brand_color":"#D34E24"},{"municipality_id":"01778.k","brand_color":"#563F1B"},{"municipality_id":"02761.q","brand_color":"#38726C"}],"customer":{"name":"Viktoria Tiedemann","date_of_birth":"1981-09-19","address":{"street":"Schönfließer Str 9","suburb":"Prenzlauer Berg","postcode":"10439"}}}}
var viewsPerRegionStyled = data.feeds.regions.map(({ name, id, suburbs }) => ({
label: name,
total: suburbs.reduce((a, { views }) => a + Number(views), 0),
color: data.feeds.branding.find(
({ municipality_id }) => municipality_id === id
).brand_color
}));
console.log(viewsPerRegionStyled);
数组转换为预先由branding
索引的对象,这将允许简单的对象查找,其复杂度低于municipality_id
:
.find