簡體   English   中英

如何基於對象數組創建新對象

[英]How to create a new Object based on an array of objects

我有一個國家列表對象,我想以合乎邏輯的方式組織它們

    const countries = {
        list: [
            {name: 'India', continent: 'Asia'},
            {name: 'China', continent: 'Asia'},
            {name: 'France', continent: 'Europe'},
            {name: 'Germany', continent: 'Europe'},
        ]
    }

這是想要的結果

    const countriesSorted = {
        list: [
            {Asia: ['India', 'China']},
            {Europe: ['France', 'Germany']}
        ]
    }

我已經使用 Set 和 Spread 獲取了獨特的大陸,但不知道如何繼續

    const continentSet = new Set(countries.list.map(country=>country.continent))

    const continents = [...continentSet]

您可以使用reduce函數對國家進行分組,如下所示:

 const countries = {list: [{name: 'India', continent: 'Asia'},{name: 'China', continent: 'Asia'},{name: 'France', continent: 'Europe'},{name: 'Germany', continent: 'Europe'}]}, countriesSorted = {list: Object.values(countries.list.reduce((a, {name, continent}) => { (a[continent] || (a[continent] = {[continent]: []}))[continent].push(name); return a; }, {})) }; console.log(countriesSorted);

沒有短路評估速記。

 const countries = {list: [{name: 'India', continent: 'Asia'},{name: 'China', continent: 'Asia'},{name: 'France', continent: 'Europe'},{name: 'Germany', continent: 'Europe'}]}, countriesSorted = { list: Object.values(countries.list.reduce((a, {name,continent}) => { if (!a[continent]) { a[continent] = { [continent]: [] } } a[continent][continent].push(name); return a; }, {})) }; console.log(countriesSorted);

希望這會有所幫助,

 const countries = { list: [{ name: 'India', continent: 'Asia' }, { name: 'China', continent: 'Asia' }, { name: 'France', continent: 'Europe' }, { name: 'Germany', continent: 'Europe' }, ] } const out = {}; for (const country of countries.list) { if (!Array.isArray(out[country.continent])) out[country.continent] = []; out[country.continent].push(country.name); } const countriesSorted = { list: [] }; for (const key in out) { countriesSorted.list.push({ [key]: out[key] }); } console.log(countriesSorted);

這是一個潛在的解決方案。 我已經刪除了嵌套列表,因為本示例不需要它。

const list = [
        {name: 'India', continent: 'Asia'},
        {name: 'China', continent: 'Asia'},
        {name: 'France', continent: 'Europe'},
        {name: 'Germany', continent: 'Europe'}
]

//Initialise the object.
let continents = {};

//Initialise the continent with empty array.
list.forEach(el => {
    if (!continents[el.continent]) {
        continents[el.continent] = [];
    }
})

//Push the country name to the correct continent array.
list.forEach(el => {
    continents[el.continent].push(el.name);
})

console.log(continents)

為了清晰起見需要重構,但它有效。

 const countries = { list: [ {name: 'India', continent: 'Asia'}, {name: 'China', continent: 'Asia'}, {name: 'France', continent: 'Europe'}, {name: 'Germany', continent: 'Europe'}, ] } // Get unique continents const continents = Array.from(new Set(countries.list.map(c => c.continent))) const sorted = { list: continents.map(c => ({ [c]: countries.list.map(co => co.continent === c ? co.name : undefined) .filter(e => !!e) }))} console.log(sorted)

我會像這樣嘗試

const countries = {
    list: [
        { name: 'India', continent: 'Asia' },
        { name: 'China', continent: 'Asia' },
        { name: 'France', continent: 'Europe' },
        { name: 'Germany', continent: 'Europe' },
    ]
}

let sortedArr = []

for(let obj of countries.list){
    //If the continent has not been added already add it and assign new set with current country
    if(!sortedArr[obj.continent]) sortedArr[obj.continent] = new Set().add(obj.name)
    else sortedArr[obj.continent].add(obj.name)
}
console.log(sortedArr)

這可以幫助:

const countries = {
        list: [
            {name: 'India', continent: 'Asia'},
            {name: 'China', continent: 'Asia'},
            {name: 'France', continent: 'Europe'},
            {name: 'Germany', continent: 'Europe'},
        ]
    }
    const continents=[...new Set(countries.list.map(el=>el.continent))];
    var data={}
    continents.forEach(element => {
        data[element]=countries.list.filter(obj=>obj.continent===element).map(item=>item.name)
    });
    const countriesSorted = {
        list:data
    }
    console.log(countriesSorted)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM