reduce makes that function more complex-seeming than it actually is. (reduce is hugely overused, and almost always the wrong tool vs. a simple loop unless you're doing functional programming with predefined, reusable reducer functions.) Here's the same function without the unnecessary reduce, with explanation:
function groupBy(objectArray, property) {
// The object we'll return with properties for the groups
let result = {}
// Loop through the array
for (const obj of objectArray) {
// Get the key value
let key = obj[property]
// If the result doesn't have an entry for that yet, create one
if (!result[key]) {
result[key] = []
}
// Add this entry to that entry
result[key].push(obj)
}
// Return the grouped result
return result
}
The reduce version just passes result around (as acc): reduce calls the callback with the initial value (the {} you see near the end of the reduce call) and the first entry, which the callback receives as acc and obj. Then the callback does one entry's work and returns acc, which means it receives it again on the next pass.
console.logtorch, he could turn on the lights using the debugger built into his IDE or browser.