我正在構建一個 API 并以給定的格式從我的資料庫中獲取資料。
不會有類似的重復{country: 'India', count: 2, status: 'Active'},{country: 'India', count: 1, status: 'Active'}
const dataFromDB = [
{country: 'India', count: 2, status: 'Active'}, {country: 'USA', count: 3, status: 'Recovered'},
{country: 'India', count: 2, status: 'Recovered'}, {country: 'Russia', count: 1, status: 'Active'},
{country: 'India', count: 1, status: 'Dead'}, {country: 'Brazil', count: 1, status: 'Active'},
{country: 'Canada', count: 1, status: 'Dead'}, {country: 'USA', count: 1, status: 'Active'}
]
但我想在發送之前將我的資料轉換為不同的格式。
const formatIWant = {
Brazil: {
active: 1,
dead: 0,
recovered: 0
},
Canada: {
active: 0,
dead: 1,
recovered: 0
},
India: {
active: 2,
dead: 1,
recovered: 2
},
Russia: {
active: 1,
dead: 0,
recovered: 0
},
USA: {
active: 1,
dead: 0,
recovered: 3
}
}
我該如何解決這個問題。
uj5u.com熱心網友回復:
您可以使用 .reduce()
const dataFromDB = [
{country: 'India', count: 2, status: 'Active'}, {country: 'USA', count: 3, status: 'Recovered'},
{country: 'India', count: 2, status: 'Recovered'}, {country: 'Russia', count: 1, status: 'Active'},
{country: 'India', count: 1, status: 'Dead'}, {country: 'Brazil', count: 1, status: 'Active'},
{country: 'Canada', count: 1, status: 'Dead'}, {country: 'USA', count: 1, status: 'Active'}
];
const result = dataFromDB.reduce((acc, value)=> {
const country = value.country.toLowerCase();
const status = value.status.toLowerCase();
const currentTotal = acc?.[country]?.[status] || 0;
return {
...acc,
[country]: {
...acc[country],
[status]: currentTotal value.count
}
}
}, {});
console.log(result);
uj5u.com熱心網友回復:
只需通過array普通回圈objects并使用每個資料創建一個具有不同鍵的新物件。這例如
const reformat = (dataFromDB) => {
const formatted = {};
for (const data of dataFromDB) {
formatted[data.country] = {
recovered: 0,
active: 0,
dead: 0,
...formatted[data.country],
[data.status.toLowerCase()]: data.count,
};
}
return formatted;
};
console.log(
reformat([
{ country: 'India', count: 2, status: 'Active' },
{ country: 'USA', count: 3, status: 'Recovered' },
{ country: 'India', count: 2, status: 'Recovered' },
{ country: 'Russia', count: 1, status: 'Active' },
{ country: 'India', count: 1, status: 'Dead' },
{ country: 'Brazil', count: 1, status: 'Active' },
{ country: 'Canada', count: 1, status: 'Dead' },
{ country: 'USA', count: 1, status: 'Active' },
])
);
uj5u.com熱心網友回復:
要將資料轉換為您想要的格式,我們可以創建一個物件formatIWant,然后遍歷dataFromDB,同時使用相關資料更新該物件。
這是一個簡單的實作,它將產生您正在尋找的結果。
const dataFromDB = [
{country: 'India', count: 2, status: 'Active'},
{country: 'USA', count: 3, status: 'Recovered'},
{country: 'India', count: 2, status: 'Recovered'},
{country: 'Russia', count: 1, status: 'Active'},
{country: 'India', count: 1, status: 'Dead'},
{country: 'Brazil', count: 1, status: 'Active'},
{country: 'Canada', count: 1, status: 'Dead'},
{country: 'USA', count: 1, status: 'Active'}
]
const formatIWant = {};
for(let i=0; i<dataFromDB.length; i ){
let country = dataFromDB[i].country;
let count = dataFromDB[i].count;
let status = dataFromDB[i].status;
// add entry for country if not found
!formatIWant[country] ? formatIWant[country] = {
active: 0,
dead: 0,
recovered: 0
} : '';
// update country with data
formatIWant[country][status.toLowerCase()] = count;
}
console.log(formatIWant);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336661.html
標籤:javascript 节点.js 数组 json 目的
上一篇:按包含的前綴過濾陣列
下一篇:使用條件步驟創建numpy陣列
