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

Time:04-13

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.

CodePudding user response:

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:

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)

CodePudding user response:

You can try this:

    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);

CodePudding user response:

//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);

  • Related