Could you help in understanding below line:
return { ...acc, [firstLetter]: [...(acc[firstLetter] || []), cur] };
From this:
// Write a function that takes an array of strings as argument // Group those strings by their first letter // Return an object that contains properties with keys representing first letters // The values should be arrays of strings containing only the corresponding strings // For example, the array ['Alf', 'Alice', 'Ben'] should be transformed to // { a: ['Alf', 'Alice'], b: ['Ben']} function myFunction(arr) { return arr.reduce((acc, cur) => { const firstLetter = cur.toLowerCase().charAt(0); return { ...acc, [firstLetter]: [...(acc[firstLetter] || []), cur] }; }, {}); }Test Cases:
myFunction(['Alf', 'Alice', 'Ben'])Expected
{ a: ['Alf', 'Alice'], b: ['Ben']}myFunction(['Ant', 'Bear', 'Bird'])Expected
{ a: ['Ant'], b: ['Bear', 'Bird']}myFunction(['Berlin', 'Paris', 'Prague'])Expected
{ b: ['Berlin'], p: ['Paris', 'Prague']}