
- 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
Calculate average from JSON data based on multiple filters JavaScript
Suppose, we have an array of objects like this −
const arr = [ { "SupplierName" : "John", "Category " : "A", "Points" : 3 }, { "SupplierName" : "John", "Category " : "A", "Points" : 11 }, { "SupplierName" : "John", "Category " : "A", "Points" : undefined }, { "SupplierName" : "John", "Category " : "B", "Points" : 2 }, { "SupplierName" : "John", "Category " : "B", "Points" : 6 }, { "SupplierName" : "Praveen", "Category " : "A", "Points" : 3 }, { "SupplierName" : "Praveen", "Category " : "A", "Points" : 7 } ];
We are required to write a JavaScript function that takes in one such array.
The function should do the following things −
- Group repeating suppliers into one unique separate object (by repeating, we mean objects with same "SupplierName" and "Category" field).
- Add up their points together
- Add a new "average field to each object, containing the average points of that supplier.
So, finally the output should look like −
const output = [ { "SupplierName" : "John", "Category " : "A", "Points" : 14, "Average" : 7 }, { "SupplierName" : "John", "Category " : "B", "Points" : 8, "Average" : 4 }, { "SupplierName" : "Praveen", "Category " : "A", "Points" : 10, "Average" : 5 } ];
Example
const arr = [ { "SupplierName" : "John", "Category " : "A", "Points" : 3 }, { "SupplierName" : "John", "Category " : "A", "Points" : 11 }, { "SupplierName" : "John", "Category " : "A", "Points" : undefined }, { "SupplierName" : "John", "Category " : "B", "Points" : 2 }, { "SupplierName" : "John", "Category " : "B", "Points" : 6 }, { "SupplierName" : "Praveen", "Category " : "A", "Points" : 3 }, { "SupplierName" : "Praveen", "Category " : "A", "Points" : 7 } ]; const groupAndAverage = (arr = []) => { const groups = arr.reduce((acc, obj) => { const name = obj.SupplierName + obj.Category; if (acc[name]) { if (obj.Points) (acc[name].Points += obj.Points) && ++acc[name].Average; } else { acc[name] = obj; acc[name].Average = 1; // taking 'Average' attribute as an items counter(on the first phase) }; return acc; }, {}); // getting "average of Points" const res = Object.keys(groups).map( name => { groups[name].Average = Math.round(groups[name].Points/groups[name].Average); return groups[name]; }); return res; }; console.log(JSON.stringify(groupAndAverage(arr), undefined, 4));
Output
And the output in the console will be −
[ { "SupplierName": "John", "Category ": "A", "Points": 22, "Average": 6 }, { "SupplierName": "Praveen", "Category ": "A", "Points": 10, "Average": 5 } ]
- Related Articles
- How to Average Cells Based on Multiple Criteria in Excel?
- Splitting strings based on multiple separators - JavaScript
- How to Average Data Based on Weekday or Weekend in Excel?
- JavaScript creating an array from JSON data?
- Merge JSON array date based JavaScript
- Dynamically replace data based on matched RegEx - JavaScript?
- Find the mean of multiple columns based on multiple grouping columns in R data frame.
- How to read data from JSON array using JavaScript?
- How to calculate the average in a column based on criteria in another column in Excel?
- How to combine multiple R data frames stored in multiple lists based on a common column?
- How can I filter JSON data with multiple objects?
- Average Data in Multiple Non-Contiguous Ranges in Excel
- How to create a subset of an R data frame based on multiple columns?
- How to access nested JSON property based on another property's value in JavaScript?
- How to subset rows based on criterion of multiple numerical columns in R data frame?

Advertisements