在javascript中转换JSON结构
我有来自无头CMS的简单在线商店的数据。数据如下所示:在javascript中转换JSON结构,javascript,Javascript,我有来自无头CMS的简单在线商店的数据。数据如下所示: [ { "id": 12312, "storeName": "Store1", "googleMapsUrl": "https://example1.com", "country": "Australia", "state": "New South Wales" }, { "id": 12313, "sto
[
{
"id": 12312,
"storeName": "Store1",
"googleMapsUrl": "https://example1.com",
"country": "Australia",
"state": "New South Wales"
},
{
"id": 12313,
"storeName": "Store2",
"googleMapsUrl": "https://example2.com",
"country": "Australia",
"state": "Queensland"
},
{
"id": 12314,
"storeName": "Store3",
"googleMapsUrl": "https://example3.com",
"country": "Indonesia",
"state": "Java"
},
]
这些数据将用于制作一个简单的商店定位器,按1)国家和2)州进行排序
我无法更改CMS上的任何内容。寻找通过这个平面数据对象循环的建议方法,并按国家和州列出
编辑:我创建此文件是为了提取独特的国家:
let newArray = this.$page.stores.edges.map(item =>{
let newObj = {};
newObj[item.node.country] = item.node.country;
newObj[item.node.country.state] = item.node.state;
console.log(newObj)
return newObj;
});
但是从那以后,我也不确定如何将其连接到州,并最终连接到商店。您可以通过减少商店来构建以下国家/地区地图
{
"Australia": {
"states": [
"New South Wales",
"Queensland"
]
},
"Indonesia": {
"states": [
"Java"
]
}
}
我颠倒了存储数据的顺序,这样排序实际上会有所不同
let stores=sortByCountryAndState(getData())
let locations=groupByCountryAndState(商店)
控制台日志(位置)
函数sortByCountryAndState(数据){
返回数据。排序((storeA,storeB)=>{
if(storeA==null)返回1
if(storeB==null)返回-1
让diff=storeA.country.localeCompare(storeB.country)
if(diff==0)返回storeA.state.localeCompare(storeB.state)
回差
})
}
功能组按国家/地区和状态(数据){
返回数据。减少((国家、商店、索引)=>{
let country=countries[store.country]|{}
返回对象。分配(国家、地区、{
[store.country]:Object.assign(国家{
状态:pushAndSort(country.states | |[],store.state)
})
})
}, {})
}
函数pushAndSort(arr,值){
如果(arr.indexOf(value)==-1)arr.push(value)
//return arr.sort()//在分组之前完成排序。。。
返回arr
}
函数getData(){
返回[{
“id”:12314,
“storeName”:“Store3”,
“谷歌地图搜索”:https://example3.com",
“国家”:“印度尼西亚”,
“状态”:“Java”
}, {
“id”:12313,
“storeName”:“Store2”,
“谷歌地图搜索”:https://example2.com",
“国家”:“澳大利亚”,
“州”:“昆士兰”
}, {
“id”:12312,
“storeName”:“Store1”,
“谷歌地图搜索”:https://example1.com",
“国家”:“澳大利亚”,
“州”:“新南威尔士州”
} ]
}
.as console wrapper{top:0;max height:100%!important;}
您可以通过减少存储来构建以下国家/地区地图
{
"Australia": {
"states": [
"New South Wales",
"Queensland"
]
},
"Indonesia": {
"states": [
"Java"
]
}
}
我颠倒了存储数据的顺序,这样排序实际上会有所不同
let stores=sortByCountryAndState(getData())
let locations=groupByCountryAndState(商店)
控制台日志(位置)
函数sortByCountryAndState(数据){
返回数据。排序((storeA,storeB)=>{
if(storeA==null)返回1
if(storeB==null)返回-1
让diff=storeA.country.localeCompare(storeB.country)
if(diff==0)返回storeA.state.localeCompare(storeB.state)
回差
})
}
功能组按国家/地区和状态(数据){
返回数据。减少((国家、商店、索引)=>{
let country=countries[store.country]|{}
返回对象。分配(国家、地区、{
[store.country]:Object.assign(国家{
状态:pushAndSort(country.states | |[],store.state)
})
})
}, {})
}
函数pushAndSort(arr,值){
如果(arr.indexOf(value)==-1)arr.push(value)
//return arr.sort()//在分组之前完成排序。。。
返回arr
}
函数getData(){
返回[{
“id”:12314,
“storeName”:“Store3”,
“谷歌地图搜索”:https://example3.com",
“国家”:“印度尼西亚”,
“状态”:“Java”
}, {
“id”:12313,
“storeName”:“Store2”,
“谷歌地图搜索”:https://example2.com",
“国家”:“澳大利亚”,
“州”:“昆士兰”
}, {
“id”:12312,
“storeName”:“Store1”,
“谷歌地图搜索”:https://example1.com",
“国家”:“澳大利亚”,
“州”:“新南威尔士州”
} ]
}
.as控制台包装{顶部:0;最大高度:100%!重要;}
按国家和州排序。
var数据=[{
“id”:12314,
“storeName”:“Store3”,
“谷歌地图搜索”:https://example3.com",
“国家”:“印度尼西亚”,
“状态”:“Java”
},
{
“id”:12315,
“storeName”:“Store4”,
“谷歌地图搜索”:https://example4.com",
“国家”:“澳大利亚”,
“州”:“昆士兰”
},
{
“id”:12312,
“storeName”:“Store1”,
“谷歌地图搜索”:https://example1.com",
“国家”:“澳大利亚”,
“州”:“新南威尔士州”
},
{
“id”:12313,
“storeName”:“Store2”,
“谷歌地图搜索”:https://example2.com",
“国家”:“澳大利亚”,
“州”:“昆士兰”
},
]
var存储={};
var countries=newset(data.map(item=>item.country.sort());
countries.forEach(country=>{
变量状态={};
var items=data.filter(item=>item.country==country);
新集合(items.map(item=>item.state.sort()).forEach(state=>{
states[state]=items.filter(item=>item.state==state)
.sort(item=>item.state);
});
商店[国家]=州;
});
console.log(存储['Australia']['Queensland'])代码>按国家和州排序。
var数据=[{
“id”:12314,
“storeName”:“Store3”,
“谷歌地图搜索”:https://example3.com",
“国家”:“印度尼西亚”,
“状态”:“Java”
},
{
“id”:12315,
“storeName”:“Store4”,
“谷歌地图搜索”:https://example4.com",
“国家”:“澳大利亚”,
“州”:“昆士兰”
},
{
“id”:12312,
“storeName”:“Store1”,
“谷歌地图搜索”:https://example1.com",
“国家”:“澳大利亚”,
“州”:“新南威尔士州”
},
{
“id”:12313,
“storeName”:“Store2”,
“谷歌地图搜索”:https://example2.com",
“国家”:“澳大利亚”,
“州”:“昆士兰”
},
]
var存储={};
var countries=newset(data.map(item=>item.country.sort());
countries.forEach(country=>{
变量状态={};
var items=data.filter(item=>item.country==country);
新集合(items.map(item=>item.state.sort()).forEach(state=>{
states[state]=items.filter(item=>item.state==state)
.sort(item=>item.sta