gpt4 book ai didi

javascript - 遍历包含附加对象数组的对象数组。并进行一定的计算

转载 作者:行者123 更新时间:2023-11-29 16:46:15 24 4
gpt4 key购买 nike

让我解释一下我在说什么。我有以下数组:

const days = [
{

date: '2016-12-13T00:00:00.000Z',
stats: [
{ name: 'A', sold: 34, },
{ name: 'B', sold: 3, },
{ name: 'C', sold: 26, },
],
},
{
date: '2016-12-14T00:00:00.000Z',
stats: [
{ name: 'D', sold: 34, },
{ name: 'E', sold: 3, },
{ name: 'F', sold: 26, },
],
},
{
date: '2016-12-14T00:00:00.000Z',
stats: [
{ name: 'D', sold: 34, },
{ name: 'E', sold: 3, },
{ name: 'F', sold: 26, },
],
},
];

我想做的是找到每个 stat.name 的百分比,例如。如果我们合并所有售出的值(value),我们得到:189 现在找到统计名称 B

%
(3 / 189) * 100

这会给我:1.58% 这表示该类别的已售商品的 %

理想情况下我想要的结果应该是这样的:

const result = [
{ name: 'A', sold: 34, percentage: '17,98%' },
{ name: 'B', sold: 3, percentage: '1,58%', },
{ name: 'C', sold: 26, percentage: '13,75%', },
{ name: 'D', sold: 68, percentage: '35,97%', },
{ name: 'E', sold: 6, percentage: '3,17%' },
{ name: 'F', sold 52, percentage: '27,51%' },
];

到目前为止我做了什么:

const days = [
{
date: '2016-12-13T00:00:00.000Z',
stats: [
{ name: 'A', sold: 34, },
{ name: 'B', sold: 3, },
{ name: 'C', sold: 26, },
],
},
{
date: '2016-12-14T00:00:00.000Z',
stats: [
{ name: 'D', sold: 34, },
{ name: 'E', sold: 3, },
{ name: 'F', sold: 26, },
],
},
{
date: '2016-12-14T00:00:00.000Z',
stats: [
{ name: 'D', sold: 34, },
{ name: 'E', sold: 3, },
{ name: 'F', sold: 26, },
],
},
];

let total = 0;
days.map(record => record.stats.map(category => total += category.sold)); // save the total;

const newStats = days.reduce(function (pastDay, currentDay) {
const nextStats = currentDay.stats.map(function(stat) {
const oldSold = pastDay.stats.find((old) => old.name === stat.name); // object that match by name.
let newSold;
if (oldSold) { // if matched
newSold = stat.sold + oldSold.sold // sum
} else { // don't sum anything
newSold = stat.sold
}
stat.sold = newSold;
stat.percentage = `${(newSold / total * 100).toFixed(2)}%`;
return stat;
});
return {
stats: nextStats,
};
});

console.log(newStats);

哪些输出:

{
"stats": [
{
"name": "D",
"sold": 68,
"percentage": "35.98%"
},
{
"name": "E",
"sold": 6,
"percentage": "3.17%"
},
{
"name": "F",
"sold": 52,
"percentage": "27.51%"
}
]
}

A、B、C.都不见了..

整个事情有更好的方法吗?我真的不喜欢先绘制 map 并获得总数,然后再处理其余部分。有没有办法做得更好?谢谢...

最佳答案

您可以先收集售出数量,然后使用百分比呈现数组。

var days = [{ date: '2016-12-13T00:00:00.000Z', stats: [{ name: 'A', sold: 34, }, { name: 'B', sold: 3, }, { name: 'C', sold: 26, }, ], }, { date: '2016-12-14T00:00:00.000Z', stats: [{ name: 'D', sold: 34, }, { name: 'E', sold: 3, }, { name: 'F', sold: 26, }, ], }, { date: '2016-12-14T00:00:00.000Z', stats: [{ name: 'D', sold: 34, }, { name: 'E', sold: 3, }, { name: 'F', sold: 26, }, ], }, ],
temp = Object.create(null),
result = [],
total = 0;

days.forEach(function (day) {
day.stats.forEach(function (stat) {
total += stat.sold;
temp[stat.name] = (temp[stat.name] || 0) + stat.sold;
});
}, Object.create(null));

result = Object.keys(temp).map(function (k) {
return { name: k, sold: temp[k], percentage: (temp[k] * 100 / total).toFixed(2) + '%' };
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 遍历包含附加对象数组的对象数组。并进行一定的计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41145653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com