/**
* Groups an array of strings into objects based on their first character.
* @param {string[]} strings - The array of strings to group.
* @returns {object} - An object where keys are the first characters of the strings,
* and values are arrays of strings starting with that character.
*/
function groupStrings(strings) {
const grouped = {}; // Initialize an empty object to store the grouped strings.
for (const str of strings) {
if (str) { // Check if the string is not empty
const firstChar = str[0]; // Get the first character of the string.
if (grouped[firstChar]) {
grouped[firstChar].push(str); // Add the string to the existing array for that character.
} else {
grouped[firstChar] = [str]; // Create a new array with the string for the character.
}
}
}
return grouped; // Return the grouped object.
}
Add your comment