// Implement _.gropuBy from Loadsh library
// function groupBy(collection, property) {
// // Test with invalid input
// const result1 = groupBy(1);
// console.log(result1); // Output: {}
// // Group by a custom function
// const result2 = groupBy([6.1, 2.4, 2.7, 6.8], Math.floor);
// // Output: { "2": [2.4, 2.7], "6": [6.1, 6.8] }
// // Group by string property (length of the string)
// const result3 = groupBy(["one", "two", "three"], "length");
// // Output: { "3": ["one", "two"], "5": ["three"] }
// // Group by deep property path
// const result4 = groupBy([{ a: { b: { c: 1 } } }, { a: { b: { c: 2 } } }], "a.b.c");
// // Output: { "1": [{ a: { b: { c: 1 } } }], "2": [{ a: { b: { c: 2 } } }] }
function groupBy(collection, property) {
// Ensure the collection is valid
if (typeof collection !== 'string' && !Array.isArray(collection) ) {
// Helper function to get key
const getKey = (item) => {
if (typeof property === 'function') {
} else if (typeof property === 'string') {
// Resolve deep property paths (e.g., "p.q.r")
const keys = property.split('.');
for (const key of keys) {
// Initialize the output object
// Iterate through the collection and group items by key
for (const item of collection) {
const key = getKey(item);
// Create a new group if the key doesn't exist
// Add the item to the corresponding group