按卖家对一系列销售额进行分组,从每个卖家那里获取总数,按总销售额排序

Group an array of sales by sellers, get the total from each Seller, order by total sales

我有一个这样的数组:

[
    {
        "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 分组,销售量汇总并由总销售额最多的卖家订购。

我试图通过使用 forEachs 和 reduce 来解决这个问题,但我失败了,如果有人能帮助我,那就太好了。

提前致谢。

//temp = given array
let result = temp.reduce((acc, curr) => {
    if (acc[curr.sellerName]) {
        acc[curr.sellerName] += curr.revenue;
    } else {
        acc[curr.sellerName] = curr.revenue;
    }
    return acc;
}, {});
// console.log(result, (a, b) => {
//     console.log
// });
result = Object.entries(result).map((value) => {
    return {
        sellerName: value[0],
        revenue: value[1],
    };
});

result = result.sort((a, b) => a["revenue"] - b["revenue"]);
console.log(result);

你可以试试这个:

    const newArray=[];
    for (var order of orders) {
        var index = newArray.findIndex((a)=>a.sellerName==order.sellerName);
        if (index>-1){
            newArray[index].totalRevenue+=order.revenue;
        } else {
            newArray.push({
                sellerName: order.sellerName,
                totalRevenue: order.revenue
           }) 
       }
   } 
   newArray.sort((a,b)=>b.totalRevenue-a.totalRevenue);

正如@CarySwoveland在评论中描述的那样,可以将每个卖家的收入累加到一个对象中,将对象条目转换为数组进行排序,然后根据排序后的值生成一个新的对象:

const sales = [{
    "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
  }
];

let totalsales = sales.reduce((acc, {
  sellerName,
  revenue
}) => {
  acc[sellerName] = (acc[sellerName] || 0) + revenue;
  return acc;
}, {});

totalsales = Object.entries(totalsales)
.sort((a, b) => b[1] - a[1])
.map((v) => ({ sellerName: v[0], totalRevenue: v[1] }))

console.log(totalsales)

使用简单的 for 循环按卖家名称对收入进行哈希处理(这比 reduce 更具可读性)。

const revenueByName = {};
for (const {sellerName, revenue} of data) {
    revenueByName[sellerName] = (revenueByName[sellerName] ?? 0) + revenue;
}
const result = Object.entries(revenueByName)
    .map(([sellerName, totalRevenue]) => ({sellerName, totalRevenue}))
    .sort((a, b) => b.totalRevenue - a.totalRevenue);

与其他答案中的技术相同,但折叠成一个函数:

const total = (sales) => 
  Object .entries (sales .reduce (
    (a, {sellerName: n, revenue}) => ({...a, [n] : (a[n] ?? 0) + revenue}), 
    {}
  )) .map (([sellerName, totalRevenue]) => ({sellerName, totalRevenue}))

const sales = [{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}]

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

使用reduce,我们得到这个中间格式:

{
  "Chris Gilmour": 2316.49,
  "Alanna Sumner": 5093.63,
  "Beth North": 1550.19,
  "Laura Ponce": 35.5,
  "Blade Newman": 5244.84
}

然后Object .entries变成

[
  ["Chris Gilmour", 2316.49],
  ["Alanna Sumner", 5093.63],
  ["Beth North", 1550.19],
  ["Laura Ponce", 35.5],
  ["Blade Newman", 5244.84]
]

然后 map 调用将其变成最终形式。

您可以将 map 与分组步骤组合在一起,然后只是 sort 数组

const orders=[{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}];

const result = Object.values(orders.reduce((acc, { sellerName, revenue }) => {
    acc[sellerName] ??= { sellerName, totalRevenue: 0 };
    acc[sellerName].totalRevenue += revenue;
    return acc;
}, {}))
.sort(({ totalRevenue: a }, { totalRevenue: b }) => b - a);

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

Lodash 如果你不介意的话

const orders=[{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}];

const result = _.chain(orders)
  .groupBy('sellerName')
  .map((values, key) => ({ sellerName: key, totalRevenue: _.sumBy(values, 'revenue') }))
  .sortBy('sellerName')
  .value();

console.log(result);
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>