如何在对象数组中查找特定键的总和
How to find the sum of a specific key in array of objects
我有一个具有以下结构的对象数组:
[
{
_id: 60b18e0a7ba49519c1ee63b4,
course: 'sfsdf',
earnings: 100,
creator: '607497d5f4bcc83fa1f3cedf',
month: 1,
year: 2019
},
{
_id: 60b18e1a7ba49519c1ee63b5,
course: 'sfsdf',
earnings: 200,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2019
},
{
_id: 60b18e227ba49519c1ee63b6,
course: 'sfsdf',
earnings: 300,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2019
},
{
_id: 60b18e297ba49519c1ee63b7,
course: 'sfsdf',
earnings: 400,
creator: '607497d5f4bcc83fa1f3cedf',
month: 4,
year: 2019
},
{
_id: 60b18b907ba49519c1ee63a5,
course: 'html-css',
earnings: 1000,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2021
},
{
_id: 60b18c457ba49519c1ee63a9,
course: 'html-css',
earnings: 500,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2020
}
]
我想以某种方式操作上述数组,使结果如下所示:
[
{ label: 2019, earnings: 1000 },
{ label: 2021, earnings: 1000 },
{ label: 2020, earnings: 500 },
]
其中 label
是唯一年份,earnings
是当年所有收入的总和。
问题解释:
2019 年有 4 个对象,每年的收入分别为 100、200、300、400,总计为 1000(100 + 200 + 300 + 400),因此结果数组有 { label: 2019, earnings: 1000 }
2021 年有 1 个对象,收入为 1000,因此总收入为 1000,因此结果数组有 { label: 2021, earnings: 1000 }
2020 年也以此类推。
您可以使用 findIndex
和 forEach
const response = [
{
_id: "60b18e0a7ba49519c1ee63b4",
course: 'sfsdf',
earnings: 100,
creator: '607497d5f4bcc83fa1f3cedf',
month: 1,
year: 2019
},
{
_id: "60b18e1a7ba49519c1ee63b5",
course: 'sfsdf',
earnings: 200,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2019
},
{
_id: "60b18e227ba49519c1ee63b6",
course: 'sfsdf',
earnings: 300,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2019
},
{
_id: "60b18e297ba49519c1ee63b7",
course: 'sfsdf',
earnings: 400,
creator: '607497d5f4bcc83fa1f3cedf',
month: 4,
year: 2019
},
{
_id: "60b18b907ba49519c1ee63a5",
course: 'html-css',
earnings: 1000,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2021
},
{
_id: "60b18c457ba49519c1ee63a9",
course: 'html-css',
earnings: 500,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2020
}
];
const result = [];
response.forEach(({earnings, year}) => {
const index = result.findIndex(({label}) => label === year);
if (index === -1) result.push({label: year, earnings});
else result[index].earnings += earnings;
})
console.log(result)
const result = arr.reduce((acc, obj) => {
const idx = acc.findIndex(o => o.label === obj.year);
if (idx !== -1) acc[idx].earnings += obj.earnings;
else acc.push({ label: obj.year, earnings: obj.earnings });
return acc;
}, [])
console.log(result)
我有一个具有以下结构的对象数组:
[
{
_id: 60b18e0a7ba49519c1ee63b4,
course: 'sfsdf',
earnings: 100,
creator: '607497d5f4bcc83fa1f3cedf',
month: 1,
year: 2019
},
{
_id: 60b18e1a7ba49519c1ee63b5,
course: 'sfsdf',
earnings: 200,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2019
},
{
_id: 60b18e227ba49519c1ee63b6,
course: 'sfsdf',
earnings: 300,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2019
},
{
_id: 60b18e297ba49519c1ee63b7,
course: 'sfsdf',
earnings: 400,
creator: '607497d5f4bcc83fa1f3cedf',
month: 4,
year: 2019
},
{
_id: 60b18b907ba49519c1ee63a5,
course: 'html-css',
earnings: 1000,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2021
},
{
_id: 60b18c457ba49519c1ee63a9,
course: 'html-css',
earnings: 500,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2020
}
]
我想以某种方式操作上述数组,使结果如下所示:
[
{ label: 2019, earnings: 1000 },
{ label: 2021, earnings: 1000 },
{ label: 2020, earnings: 500 },
]
其中 label
是唯一年份,earnings
是当年所有收入的总和。
问题解释:
2019 年有 4 个对象,每年的收入分别为 100、200、300、400,总计为 1000(100 + 200 + 300 + 400),因此结果数组有
{ label: 2019, earnings: 1000 }
2021 年有 1 个对象,收入为 1000,因此总收入为 1000,因此结果数组有
{ label: 2021, earnings: 1000 }
2020 年也以此类推。
您可以使用 findIndex
和 forEach
const response = [
{
_id: "60b18e0a7ba49519c1ee63b4",
course: 'sfsdf',
earnings: 100,
creator: '607497d5f4bcc83fa1f3cedf',
month: 1,
year: 2019
},
{
_id: "60b18e1a7ba49519c1ee63b5",
course: 'sfsdf',
earnings: 200,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2019
},
{
_id: "60b18e227ba49519c1ee63b6",
course: 'sfsdf',
earnings: 300,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2019
},
{
_id: "60b18e297ba49519c1ee63b7",
course: 'sfsdf',
earnings: 400,
creator: '607497d5f4bcc83fa1f3cedf',
month: 4,
year: 2019
},
{
_id: "60b18b907ba49519c1ee63a5",
course: 'html-css',
earnings: 1000,
creator: '607497d5f4bcc83fa1f3cedf',
month: 3,
year: 2021
},
{
_id: "60b18c457ba49519c1ee63a9",
course: 'html-css',
earnings: 500,
creator: '607497d5f4bcc83fa1f3cedf',
month: 2,
year: 2020
}
];
const result = [];
response.forEach(({earnings, year}) => {
const index = result.findIndex(({label}) => label === year);
if (index === -1) result.push({label: year, earnings});
else result[index].earnings += earnings;
})
console.log(result)
const result = arr.reduce((acc, obj) => {
const idx = acc.findIndex(o => o.label === obj.year);
if (idx !== -1) acc[idx].earnings += obj.earnings;
else acc.push({ label: obj.year, earnings: obj.earnings });
return acc;
}, [])
console.log(result)