Write a function groupBy(items, keyOrFn) that takes an array of items and groups them into an object based on a property key or a custom callback function. For example, groupBy(['apple', 'banana', 'avocado'], word => word[0]) should return { a: ['apple', 'avocado'], b: ['banana'] }.
function groupBy(items, keyOrFn) {
// your code here
}
Rules:
- Support both a string key and a callback function as the second argument.
- Return a plain JavaScript object mapping group keys to arrays of matching items.
- Do not use external libraries or Object.groupBy.
Post your solution as a reply. Answer goes up in about a day.