JavaScript: Group the elements of an array based on the given function and returns the count of elements in each group
JavaScript fundamental (ES6 Syntax): Exercise-69 with Solution
Group Array and Count Elements
Write a JavaScript program to group array elements based on the given function. It return the count of elements in each group.
- Use Array.prototype.map() to map the values of an array to a function or property name.
- Use Array.prototype.reduce() to create an object, where the keys are produced from the mapped results.
Sample Solution:
JavaScript Code:
Output:
{"10":1,"2.449489742783178":1,"3.1622776601683795":2} {"4":1,"6":2} {"3":2,"5":1}
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that groups array elements by a specified key and returns the count for each group.
- Write a JavaScript function that creates a frequency map of array elements using a grouping function.
- Write a JavaScript program that processes an array and outputs an object with counts of elements grouped by a predicate.
Go to:
PREV : Converge Function with Branching Functions.
NEXT : Count Value in Array.
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.