Javascript 如何将一组对象过滤为单个对象并按其关键帧进行组织?
我试图筛选一组对象并返回一个对象,按键Javascript 如何将一组对象过滤为单个对象并按其关键帧进行组织?,javascript,arrays,Javascript,Arrays,我试图筛选一组对象并返回一个对象,按键大陆、国家和城市组织,然后将这些对象放置在正确的键中 const data = [{ location: [{ name: 'Africa', type: 'Continent' }, { name: 'Angola', type: 'Country' }, { name: 'Luanda', type: 'City' } ], val
大陆
、国家
和城市
组织,然后将这些对象放置在正确的键中
const data = [{
location: [{
name: 'Africa',
type: 'Continent'
},
{
name: 'Angola',
type: 'Country'
},
{
name: 'Luanda',
type: 'City'
}
],
values: []
}, {
location: [{
name: 'Europe',
type: 'Continent'
},
{
name: 'Italy',
type: 'Country'
},
{
name: 'Rome',
type: 'City'
}
],
values: []
}, {
location: [{
name: 'Europe',
type: 'Continent'
},
{
name: 'Spain',
type: 'Country'
},
{
name: 'Valencia',
type: 'City'
}
],
values: []
}]
它应导致:
{
Africa: {
countries: {
Angola: {
cities: {
Luanda: {
values: []
}
}
}
}
},
Europe: {
countries: {
Italy: {
cities: {
Rome: {
values: []
}
}
},
Spain: {
cities: {
Valencia: {
values: []
}
}
}
}
}
}
我尝试过按按键过滤,但当要将对象放置在正确的位置(例如同一个大陆)时,我无法让它工作
const result = data.reduce((acc, total) => {
const continentFilter = total.location.find(s => s.type === 'Continent')
acc[continentFilter.name] = {
// ...
}
return acc
}, {})
console.log(result)
// {
// Africa: { ... },
// Europe: { ... }
// }
更新:
- 类型始终为“大陆”、“国家”或“城市”
- 某些大陆/国家可能没有城市
- 目标是按
,然后按大陆
和国家
城市
类型
)
然后迭代数组并创建嵌套结构。最后应用这些值
对于未排序的位置,请提前排序
const
数据=[{location:[{name:'安哥拉',type:'国家'},{name:'罗安达',type:'城市'},{name:'非洲',type:'大陆'},{location:[{name:'欧洲',type:'大陆'},{name:'意大利',type:'国家'},{name:'罗马',type:'城市'},value:[]},{location:[{name:'欧洲',type:'大陆'},{name:'spaine',type:'Country'},{name:'Valencia',type:'City'}],值:[]};
级别=[“国家”、“城市”],
结果=数据.reduce((r,{location,values})=>{
常数
顺序={大陆:1,国家:2,城市:3},
温度=位置
.sort((a,b)=>顺序[a.type]-顺序[b.type])
.reduce((o,{name},i)=>{
o[名称]??={};
回报水平[i]
?(o[名称][级别[i]]??={})
:o[姓名];
},r);
(温度值???=[])。推送(…值);
返回r;
}, {});
console.log(结果);
.as console wrapper{max height:100%!important;top:0;}
您可以为位置数组未提供的嵌套属性获取一个数组(可能存在键而不是未使用的类型
)
然后迭代数组并创建嵌套结构。最后应用值
对于未排序的位置,请提前排序
const
数据=[{location:[{name:'安哥拉',type:'国家'},{name:'罗安达',type:'城市'},{name:'非洲',type:'大陆'},{location:[{name:'欧洲',type:'大陆'},{name:'意大利',type:'国家'},{name:'罗马',type:'城市'},value:[]},{location:[{name:'欧洲',type:'大陆'},{name:'spaine',type:'Country'},{name:'Valencia',type:'City'}],值:[]};
级别=[“国家”、“城市”],
结果=数据.reduce((r,{location,values})=>{
常数
顺序={大陆:1,国家:2,城市:3},
温度=位置
.sort((a,b)=>顺序[a.type]-顺序[b.type])
.reduce((o,{name},i)=>{
o[名称]??={};
回报水平[i]
?(o[名称][级别[i]]??={})
:o[姓名];
},r);
(温度值???=[])。推送(…值);
返回r;
}, {});
console.log(结果);
.as控制台包装{max height:100%!important;top:0;}
这里是一个工作示例。我相信它可以改进
const数据=[{
地点:[{
名称:“非洲”,
类型:“大陆”
},
{
名称:“安哥拉”,
类型:“国家”
},
{
名称:“罗安达”,
类型:“城市”
}
],
值:[]
}, {
地点:[{
名称:"欧洲",,
类型:“大陆”
},
{
名称:“意大利”,
类型:“国家”
},
{
名称:“罗马”,
类型:“城市”
}
],
值:[]
}, {
地点:[{
名称:"欧洲",,
类型:“大陆”
},
{
名称:“西班牙”,
类型:“国家”
},
{
名称:“瓦伦西亚”,
类型:“城市”
}
],
值:[]
}];
函数countryDataToStructuredObject(数据){
让countryData={};
data.map(a=>{
countryData[a.location.find(x=>x.type=='Continental').name]={};
countryData[a.location.find(x=>x.type=='大陆').name]['countries']={};
countryData[a.location.find(x=>x.type=='Continental').name]['countries'][a.location.find(x=>x.type=='Country').name]={};
countryData[a.location.find(x=>x.type=='Continental').name]['countries'][a.location.find(x=>x.type=='Country').name]['cities']={};
countryData[a.location.find(x=>x.type=='Continental').name]['countries'][a.location.find(x=>x.type=='Country').name]['cities'][a.location.find(x=>x.type==''City').name]={values:a.values};
});
返回国家数据;
}
console.log(countryDataToStructuredObject(data));
这里有一个工作示例。我相信它可以改进
const数据=[{
地点:[{
名称:“非洲”,
类型:“大陆”
},
{
名称:“安哥拉”,
类型:“国家”
},
{
名称:“罗安达”,
类型:“城市”
}
],
值:[]
}, {
地点:[{
名称:"欧洲",,
类型:“大陆”
},
{
名称:“意大利”,
类型:“国家”
},
{
名称:“罗马”,
类型:“城市”
}
],
值:[]
}, {
地点:[{
名称:"欧洲",,
类型:“大陆”
},
{
名称:“西班牙”,
类型:“国家”
},
{
名称:“瓦伦西亚”,
类型:“城市”
}
],
值:[]
}];
函数countryDataToStructuredObject(数据){
让countryData={};
data.map(a=>{
countryData[a.location.find(x=>x.type=='Continental').name]={};
countryData[a.location.find(x=>x.type='Con