
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sum from array values with similar key in JavaScript
Let’s say, here is an array that contains some data about the stocks sold and purchased by some company over a period of time.
const transactions = [ ['AAPL', 'buy', 100], ['WMT', 'sell', 75], ['MCD', 'buy', 125], ['GOOG', 'sell', 10], ['AAPL', 'buy', 100], ['AAPL', 'sell', 100], ['AAPL', 'sell', 20], ['DIS', 'buy', 15], ['MCD', 'buy', 10], ['WMT', 'buy', 50], ['MCD', 'sell', 90] ];
We want to write a function that takes in this data and returns an object of arrays with key as stock name (like ‘AAPL’, ‘MCD’) and value as array of two numbers, where the first element represents the total buy and second element represents the total sell. Therefore, the code for doing this will be −
Example
const transactions = [ ['AAPL', 'buy', 100], ['WMT', 'sell', 75], ['MCD', 'buy', 125], ['GOOG', 'sell', 10], ['AAPL', 'buy', 100], ['AAPL', 'sell', 100], ['AAPL', 'sell', 20], ['DIS', 'buy', 15], ['MCD', 'buy', 10], ['WMT', 'buy', 50], ['MCD', 'sell', 90] ]; const digestTransactions = (arr) => { return arr.reduce((acc, val, ind) => { const [stock, type, amount] = val; if(acc[stock]){ const [buy, sell] = acc[stock]; if(type === 'buy'){ acc[stock] = [buy+amount, sell]; }else{ acc[stock] = [buy, sell+amount]; } }else{ if(type === 'buy'){ acc[stock] = [amount, 0]; }else{ acc[stock] = [0, amount]; } } return acc; }, {}); }; console.log(digestTransactions(transactions));
Output
The output in the console will be −
{ AAPL: [ 200, 120 ], WMT: [ 50, 75 ], MCD: [ 135, 90 ], GOOG: [ 0, 10 ], DIS: [ 15, 0 ] }
- Related Articles
- Merge objects in array with similar key JavaScript
- Sum similar numeric values within array of objects - JavaScript
- Retrieve key and values from object in an array JavaScript
- Sum values in MySQL from similar day records
- Sum all similar elements in one array - JavaScript
- JavaScript reduce sum array with undefined values
- How to return object from an array with highest key values along with name - JavaScript?
- Remove duplicates from array with URL values in JavaScript
- Sum all duplicate values in array in JavaScript
- Filter unique array values and sum in JavaScript
- MongoDB query to sum specific key values with terminal commands?
- Sum of nested object values in Array using JavaScript
- Finding the sum of unique array values - JavaScript
- Sum columns corresponding values according to similar dates in MySQL?
- Sum of array object property values in new array of objects in JavaScript

Advertisements