- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Grouping an Array and Counting items creating new array based on Groups in JavaScript
Suppose, we have an array of objects like this −
const arr = [ { region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" } ];
We are required to write a JavaScript function that takes in one such array. The function should prepare a new array of objects that groups the data based on the "area" property of objects.
The function should also keep a count of the unique users for a particular area.
Therefore, for the above array, the output should look like −
const output = [ { "region": "Africa", "count": 2 }, { "region": "Europe", "count": 2 }, { "region": "Asia", "count": 2 } ];
Example
The code for this will be −
const arr = [ { region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" } ]; const groupByArea = (arr = []) => { const res = []; arr.forEach(el => { let key = [el.region, el.user].join('|'); if (!this[el.region]) { this[el.region] = { region: el.region, count: 0 }; res.push(this[el.region]); }; if (!this[key]) { this[key] = true; this[el.region].count++; }; }, Object.create(null)); return res; } console.log(groupByArea(arr));
Output
And the output in the console will be −
[ { region: 'Africa', count: 2 }, { region: 'Europe', count: 2 }, { region: 'Asia', count: 2 } ]
- Related Articles
- Creating an array of objects based on another array of objects JavaScript
- Modify an array based on another array JavaScript
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- Creating a JavaScript array with new keyword.
- Filter an object based on an array JavaScript
- Shuffling string based on an array in JavaScript
- Grouping array of array on the basis of elements in JavaScript
- Sort array based on another array in JavaScript
- Filter array based on another array in JavaScript
- Sorting Array based on another array JavaScript
- Shifting string letters based on an array in JavaScript
- map() array of object titles into a new array based on other property value JavaScript
- Creating an associative array in JavaScript?
- Splitting an array into groups in JavaScript
- Reduce an array to groups in JavaScript

Advertisements