[英]How to filter an array of objects into a single object and organised by its keys?
我正在尝试过滤一组对象并返回一个 object,由键continents
、 countries
和cities
组织,我们将这些对象放在正确的键中。
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: { ... }
// }
更新:
Continent
组织,然后按Country
和City
组织 您可以为位置 arrays 未给出的嵌套属性获取一个数组(也许可能有 key 而不是未使用的type
)。
然后迭代数组并创建一个嵌套结构。 最后应用这些值。
对于未排序的位置,请提前对其进行排序。
const data = [{ location: [{ name: 'Angola', type: 'Country' }, { name: 'Luanda', type: 'City' }, { name: 'Africa', type: 'Continent' }], 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: [] }]; levels = ['countries', 'cities'], result = data.reduce((r, { location, values }) => { const order = { Continent: 1, Country: 2, City: 3 }, temp = location.sort((a, b) => order[a.type] - order[b.type]).reduce((o, { name }, i) => { o[name]??= {}; return levels[i]? (o[name][levels[i]]??= {}): o[name]; }, r); (temp.values??= []).push(...values); return r; }, {}); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
这是一个工作示例。 我确信它可以改进。
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: [] }]; function countryDataToStructuredObject(data){ let countryData = { }; data.map(a => { countryData[a.location.find(x => x.type == 'Continent').name] = {}; countryData[a.location.find(x => x.type == 'Continent').name]['countries'] = {}; countryData[a.location.find(x => x.type == 'Continent').name]['countries'][a.location.find(x => x.type == 'Country').name] = {}; countryData[a.location.find(x => x.type == 'Continent').name]['countries'][a.location.find(x => x.type == 'Country').name]['cities'] = {}; countryData[a.location.find(x => x.type == 'Continent').name]['countries'][a.location.find(x => x.type == 'Country').name]['cities'][a.location.find(x => x.type == 'City').name] = { values: a.values }; }); return countryData; } console.log(countryDataToStructuredObject(data));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.