[英]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.