javascript - group array items by
JavaScript[Edit]
+
0
-
0
JavaScript - group array items by
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50const groupItems = (array, keyGetter) => { const access = {}; // lets to access group items quickly const groups = []; for (const item of array) { const key = keyGetter(item); if (key == null) { // null or undefined continue; } const items = access[key]; if (items) { items.push(item); } else { const items = [item]; access[key] = items; groups.push({key, items}); } } return groups; }; // Usage example: const users = [ {id: 1, name: 'John', age: 21}, {id: 2, name: 'Kate', age: 23}, {id: 3, name: 'Matt', age: 21}, ]; const groups = groupItems(users, (user) => user.age); // groups users by age console.log(groups); // Output: // // [ // { // key: 21, // items: [ // {id: 1, name: 'John', age: 21}, // {id: 3, name: 'Matt', age: 21} // ] // }, // { // key: 23, // items: [ // {id: 2, name: 'Kate', age: 23} // ] // } // ]
Reset
[Edit]
+
0
-
0
JavaScript - group array items by
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42const groupItems = (array, keyGetter) => { const groups = {}; for (const item of array) { const key = keyGetter(item); if (key == null) { // null or undefined continue; } const group = groups[key] ?? (groups[key] = []); group.push(item); } return groups; }; // Usage example: const users = [ {id: 1, name: 'John', age: 21}, {id: 2, name: 'Kate', age: 23}, {id: 3, name: 'Matt', age: 21}, ]; const groups = groupItems(users, (user) => user.age); // groups users by age console.log(groups); // Output: // // { // '21': [ // {id: 1, name: 'John', age: 21}, // {id: 3, name: 'Matt', age: 21} // ], // '23': [ // {id: 2, name: 'Kate', age: 23} // ] // } // See also: // // 1. https://dirask.com/snippets/JavaScript-group-array-items-by-pJZb9p
[Edit]
+
0
-
0
JavaScript - group array items by
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41const groupItems = (array, keyGetter) => { const reducer = (groups, item) => { const key = keyGetter(item); if (key != null) { // not null and not undefined const group = groups[key] ?? (groups[key] = []); group.push(item); } return groups; }; return array.reduce(reducer, {}); }; // Usage example: const users = [ {id: 1, name: 'John', age: 21}, {id: 2, name: 'Kate', age: 23}, {id: 3, name: 'Matt', age: 21}, ]; const groups = groupItems(users, (user) => user.age); // groups users by age console.log(groups); // Output: // // { // '21': [ // {id: 1, name: 'John', age: 21}, // {id: 3, name: 'Matt', age: 21} // ], // '23': [ // {id: 2, name: 'Kate', age: 23} // ] // } // See also: // // 1. https://dirask.com/snippets/JavaScript-group-array-items-by-pJZb9p
[Edit]
+
0
-
0
JavaScript - group array items by
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29// Warning: Object.groupBy() method was introduced in 2023 in the major web browsers, Node.js and Deno. const users = [ {id: 1, name: 'John', age: 21}, {id: 2, name: 'Kate', age: 23}, {id: 3, name: 'Matt', age: 21}, ]; const groups = Object.groupBy(users, (user) => user.age); // groups users by age console.log(groups); // Output: // // { // '21': [ // {id: 1, name: 'John', age: 21}, // {id: 3, name: 'Matt', age: 21} // ], // '23': [ // {id: 2, name: 'Kate', age: 23} // ] // } // See also: // // 1. https://dirask.com/snippets/JavaScript-group-array-items-by-pJZb9p