Skip to content

Instantly share code, notes, and snippets.

@benwells
Created May 12, 2016 13:40
Show Gist options
  • Save benwells/0111163b3cccfad0804d994c70de7aa1 to your computer and use it in GitHub Desktop.
Save benwells/0111163b3cccfad0804d994c70de7aa1 to your computer and use it in GitHub Desktop.
Using Array.reduce to sum a property in an array of objects
var accounts = [
{ name: 'James Brown', msgCount: 123 },
{ name: 'Stevie Wonder', msgCount: 22 },
{ name: 'Sly Stone', msgCount: 16 },
{ name: 'Otis Redding', msgCount: 300 } // Otis has the most messages
];
// get sum of msgCount prop across all objects in array
var msgTotal = accounts.reduce(function(prev, cur) {
return prev + cur.msgCount;
}, 0);
console.log('Total Messages:', msgTotal); // Total Messages: 461
@Elijah23Johnson
Copy link

var itemsInCart= [ { name: "Suya", price: 69, qty: 3 }, { name: "Matooke", price: 46, qty: 2 } ]; var total = itemsInCart.reduce(function(prev, cur) { return prev +(cur.price * cur.qty); }, 0); document.write('Total of Cart Items:', total);

@barak2018
Copy link

barak2018 commented Aug 30, 2019

ok, i want to do totalbill by mentioning specific name. i mean, suya and matooke. i want a program to firstly track the name of item and go on with calculation.
so, my challenge is how i can do it with the reduce function.
i mean, if the program doesn't find suya and mattoke, it stops working.and most importantly, use ES6 syntax.
look at the apiendpoint https://randomapi.com/api/006b08a801d82d0c9824dcfdfdfa3b3c

@barak2018
Copy link

var itemsInCart= [ { name: "Suya", price: 69, qty: 3 }, { name: "Matooke", price: 46, qty: 2 } ]; var total = itemsInCart.reduce(function(prev, cur) { return prev +(cur.price * cur.qty); }, 0); document.write('Total of Cart Items:', total);

thank u very much

ok, i want to do totalbill by mentioning specific name. i mean, suya and matooke. i want a program to firstly track the name of item and go on with calculation.
so, my challenge is how i can do it with the reduce function.
i mean, if the program doesn't find suya and mattoke, it stops working.and most importantly, use ES6 syntax.
look at the apiendpoint https://randomapi.com/api/006b08a801d82d0c9824dcfdfdfa3b3c

@oskarhertzman
Copy link

Thanks dude

@Faizanq
Copy link

Faizanq commented May 5, 2020

Thanks bro

@ctavolazzi
Copy link

Hey, thanks for sharing this! Very helpful. Answers a burning question I've been working on for the last half hour. Thank you!

@AlanConcepcion
Copy link

Thanks dude

@workingwebsites
Copy link

Very nice! Thank you!

@zloyded
Copy link

zloyded commented Jan 17, 2021

items = [
  { customer_id: 1, id: 1, sum: 123}
  { customer_id: 1, id: 2, sum: 321}
  { customer_id: 1, id: 3, sum: 213},
]
var total_sum = items.reduce(function(prev, cur) {
      return prev + cur.summ;
}, 0);

console.log(total_sum) -> `NaN` %(

@mukolweke
Copy link

Very nice. Thanks

@marvinwarden
Copy link

Thank you!!

@rikoz
Copy link

rikoz commented Jun 4, 2021

Simple and short... thanks

@everaldomatias
Copy link

Great.

How to sum value only when the key 'name' that duplicate, sample:

var accounts = [
  { name: 'Blue', count: 2 },
  { name: 'Red', count: 3 },
  { name: 'Yellow', count: 4 },
  { name: 'Red', count: 5 }
];

And return:

var return = [
  { name: 'Blue', count: 2 },
  { name: 'Red', count: 8 },
  { name: 'Yellow', count: 4 }
];

@mikalcallahan
Copy link

@benwells @perdugames very clutch, thanks!

@James-n9ne4our
Copy link

James-n9ne4our commented Jul 11, 2021

@everaldomatias have you found any solution to that please???

@everaldomatias
Copy link

@James-n9ne4our i still haven't found the solution.

@RihabBerry
Copy link

this is the genius solution!

@leonelmatsinhe
Copy link

In ES6: const msgTotal = accounts.reduce((prev, cur) => prev + cur.msgCount, 0);

Thanks dude!

@stoicalpirate
Copy link

Thanks - super useful!

@saurav2811
Copy link

Is it possible to use reduce if I have a calculation trough-out multiple properties like this:

var items = [
  { name: 'Item 1', amount: 123, discount: 0.1 },
  { name: 'Item 2', amount: 22, discount: 0.1 },
  { name: 'Item 3', amount: 16, discount: 0.2 },
  { name: 'Item 4', amount: 300, discount: 0 }
];

So I have to do something like this:

total = sum_all_items(amount - (amount * discount))

simplify it as total = sum_all_items(amount * (1 - discount))
OR
first, just make, discount = 1 - discount, and then, sum(amount * discount)

@Bizzle-Dapp
Copy link

Banging. Nice one, mate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment