Skip to content

Instantly share code, notes, and snippets.

@Chalarangelo
Created September 14, 2017 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chalarangelo/59afa300bee8c588b8a10e4c10bc9840 to your computer and use it in GitHub Desktop.
Save Chalarangelo/59afa300bee8c588b8a10e4c10bc9840 to your computer and use it in GitHub Desktop.
// Arrays of expenses.
var oldExpenses = [
{ company: "BigCompany Co.", value: 1200.10},
{ company: "Pineapple Inc.", value: 3107.02},
{ company: "Office Supplies Inc.", value: 266.97}
];
var newExpenses = [
{ company: "Office Supplies Inc.", value: 108.11},
{ company: "Megasoft Co.", value: 1208.99}
];
// Simple summation function
var sumValues = function(sum, x){
return sum + x.value;
}
// Reducing the first array to a sum of values.
var oldExpensesSum = oldExpenses.reduce(sumValues, 0.0);
// Reducing the second array to a sum of values.
console.log(newExpenses.reduce(sumValues, oldExpensesSum)); // 5891.19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment