const groupBy = (arr, key) => { return arr.reduce((prev, curr) => { (prev[curr[key]] = prev[curr[key]] || []).push(curr); return prev; }, {}); } const arr = [ { id: 1, name: "Alice", age: 20 }, { id: 2, name: "Bob", age: 18 }, { id: 3, name: "Charlie", age: 20 } ]; const result = groupBy(arr, "age"); console.log(result); /* { "18": [{ id: 2, name: "Bob", age: 18 }], "20": [ { id: 1, name: "Alice", age: 20 }, { id: 3, name: "Charlie", age: 20 } ] } */