Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

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.

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

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)

about 4 years ago · Santiago Gelvez Denunciar

0

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);
about 4 years ago · Santiago Gelvez Denunciar

0

Same technique as in other answers, but folded into a single function:

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}

Using reduce, we get this intermediate format:

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

Then Object .entries turns it into

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

and the map call turns it into its final form.

about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda