卖方的一系列销售组,从每个卖方那里获取总数,按总销售额订购
我有这样的数组:
[
{
"orderId": 1,
"orderDate": "2021-04-28T08:20:58Z",
"status": "Confirmed",
"sellerName": "Chris Gilmour",
"revenue": 2316.49
},
{
"orderId": 2,
"orderDate": "2020-12-19T12:30:18Z",
"status": "Confirmed",
"sellerName": "Alanna Sumner",
"revenue": 2928.88
},
{
"orderId": 4,
"orderDate": "2020-12-24T08:00:09Z",
"status": "Confirmed",
"sellerName": "Beth North",
"revenue": 1550.19
},
{
"orderId": 5,
"orderDate": "2021-06-06T04:40:48Z",
"status": "Confirmed",
"sellerName": "Laura Ponce",
"revenue": 35.5
},
{
"orderId": 8,
"orderDate": "2021-08-27T05:13:40Z",
"status": "Canceled",
"sellerName": "Blade Newman",
"revenue": 2957.29
},
{
"orderId": 9,
"orderDate": "2020-12-26T08:07:57Z",
"status": "Confirmed",
"sellerName": "Alanna Sumner",
"revenue": 2164.75
},
{
"orderId": 10,
"orderDate": "2021-04-23T18:44:19Z",
"status": "Confirmed",
"sellerName": "Blade Newman",
"revenue": 2287.55
}
]
我希望新阵列成为:
[
{
"sellerName": "Blade Newman",
"totalRevenue": 5244.84
},
{
"sellerName": "Alanna Sumner",
"totalRevenue": 5093.63
},
{
"sellerName": "Chris Gilmour",
"totalRevenue" : 2316.49
},
{
"sellerName": "Beth North",
"totalRevenue": 1550.19
}
]
所以我希望该数组由Sellername分组,售出的金额总结并由销售总数最多的卖家订购。
我试图通过使用foreach和降低来解决这个问题,但是如果有人能帮助我,那我就会很棒。
提前致谢。
I have an array like this :
[
{
"orderId": 1,
"orderDate": "2021-04-28T08:20:58Z",
"status": "Confirmed",
"sellerName": "Chris Gilmour",
"revenue": 2316.49
},
{
"orderId": 2,
"orderDate": "2020-12-19T12:30:18Z",
"status": "Confirmed",
"sellerName": "Alanna Sumner",
"revenue": 2928.88
},
{
"orderId": 4,
"orderDate": "2020-12-24T08:00:09Z",
"status": "Confirmed",
"sellerName": "Beth North",
"revenue": 1550.19
},
{
"orderId": 5,
"orderDate": "2021-06-06T04:40:48Z",
"status": "Confirmed",
"sellerName": "Laura Ponce",
"revenue": 35.5
},
{
"orderId": 8,
"orderDate": "2021-08-27T05:13:40Z",
"status": "Canceled",
"sellerName": "Blade Newman",
"revenue": 2957.29
},
{
"orderId": 9,
"orderDate": "2020-12-26T08:07:57Z",
"status": "Confirmed",
"sellerName": "Alanna Sumner",
"revenue": 2164.75
},
{
"orderId": 10,
"orderDate": "2021-04-23T18:44:19Z",
"status": "Confirmed",
"sellerName": "Blade Newman",
"revenue": 2287.55
}
]
I want the new Array to be :
[
{
"sellerName": "Blade Newman",
"totalRevenue": 5244.84
},
{
"sellerName": "Alanna Sumner",
"totalRevenue": 5093.63
},
{
"sellerName": "Chris Gilmour",
"totalRevenue" : 2316.49
},
{
"sellerName": "Beth North",
"totalRevenue": 1550.19
}
]
So I want the array to be grouped by sellerName, the amount sold summed up and ordered By sellers with the most total sales.
I tried to solve this by using forEachs and reduces but I failed, if anyone can help me that would be great.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如@CarySwoveland在评论中所述,您可以在对象中为每个卖方积累收入,将对象条目转换为数组进行排序,然后根据排序值生成一个新对象:
As described by @CarySwoveland in the comments, you can accumulate revenue for each seller in an object, convert the object entries to an array to sort, and then generate a new object based on the sorted values:
您可以尝试一下:
You can try this:
使用一个简单的循环循环以卖方名称获得收入(这比
redy
更可读)。Use a simple
for
loop to make a hash of revenue by seller name (this is more readable thanreduce
).与其他答案相同的技术,但折叠成一个功能:
使用
降低
,我们获得此中间格式:然后
对象.entries
将其变成map
调用将其变成其最终形式。Same technique as in other answers, but folded into a single function:
Using
reduce
, we get this intermediate format:Then
Object .entries
turns it intoand the
map
call turns it into its final form.您可以将
MAP
与分组步骤组合在一起,然后只有sort
数组You can combine
map
with grouping step together, and then justsort
arraylodash 如果您不介意
Lodash if you don't mind