将对象数组的结构更改为所需的格式



我正在构建一个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
}
}

如何解决这个问题?

要将数据转换为所需的格式,我们可以创建一个对象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);

您可以使用.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 defaultStatus = dataFromDB.reduce((acc, {status}) =>
acc.hasOwnProperty(status.toLowerCase())
? acc
: {...acc, [status.toLowerCase()]: 0}
, {});
const result = dataFromDB.reduce((acc, value)=> {
const country = value.country.toLowerCase();
const status = value.status.toLowerCase();
return {
...acc,
[country]: {
...defaultStatus,
...acc[country],
[status]: value.count
}
}
}, {});
console.log(result);

循环遍历普通objectsarray并创建一个新的对象,使用每个数据使用不同的键。例如

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' },
])
);

最新更新