
- 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
Grouping names based on first letter in JavaScript
Suppose, we have an array of names like this −
const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"];
We are required to write a JavaScript function that takes in one such array. The function should return an array of objects with two properties −
letter -> the letter on which the names are grouped
names -> an array of names that falls in that group
Example
The code for this will be −
const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"]; const groupNames = arr => { const map = arr.reduce((acc, val) => { let char = val.charAt(0).toUpperCase(); acc[char] = [].concat((acc[char] || []), val); return acc; }, {}); const res = Object.keys(map).map(el => ({ letter: el, names: map[el] })); return res; }; console.log(groupNames(arr));
Output
The output in the console −
[ { letter: 'S', names: [ 'Simon', 'Susi' ] }, { letter: 'M', names: [ 'Mike' ] }, { letter: 'J', names: [ 'Jake', 'James' ] }, { letter: 'L', names: [ 'Lara' ] }, { letter: 'B', names: [ 'Blake' ] } ]
- Related Articles
- Delete duplicate elements based on first letter – JavaScript
- Grouping objects based on key property in JavaScript
- Grouping an Array and Counting items creating new array based on Groups in JavaScript
- Splitting an array based on its first value - JavaScript
- Array filtering using first string letter in JavaScript
- Capitalizing first letter of each word JavaScript
- Make first letter of a string uppercase in JavaScript?
- MySQL query for grouping and summing the values based on specific records
- Grouping on the basis of object property JavaScript
- How to subset a named vector based on names in R?
- Display resultant array based on the object’s order determined by the first array in JavaScript?
- Find the mean of multiple columns based on multiple grouping columns in R data frame.
- Convert a data frame with grouping column into a list based on groups in R.
- Array grouping on the basis of children object’s property in JavaScript
- Grouping array of array on the basis of elements in JavaScript

Advertisements