https://github.com/codex-central/grouping
Functions to group data by a key or multiple keys.
https://github.com/codex-central/grouping
array group javascript object typescript
Last synced: about 2 months ago
JSON representation
Functions to group data by a key or multiple keys.
- Host: GitHub
- URL: https://github.com/codex-central/grouping
- Owner: Codex-Central
- License: apache-2.0
- Created: 2024-05-07T16:01:55.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-07T18:08:05.000Z (about 2 years ago)
- Last Synced: 2025-06-09T15:58:18.013Z (about 1 year ago)
- Topics: array, group, javascript, object, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@codexcentral/grouping
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# grouping
Functions to group data by a key or multiple keys.
## Installation
> `npm install @codexcentral/grouping`
## Importing
### Javascript
```javascript
const { groupBy } = require("@codexcentral/grouping");
```
### Typescript
```typescript
import { groupBy } from "@codexcentral/grouping";
```
## Functions
-
groupBy
Groups an array of objects by a key.
| Parameter | Type | Description |
| --------- | ------ | ------------------------- |
| data | Array | Array of objects to group |
| key | String | Key to group by |
#### Example
#### Javascript
```javascript
const { groupBy } = require('@codexcentral/grouping');
...
const people = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 25 }
];
console.log(groupBy(people, 'age'));
// Output:
// {
// '25': [
// { name: 'Alice', age: 25 },
// { name: 'Charlie', age: 25 }
// ],
// '30': [
// { name: 'Bob', age: 30 }
// ]
// }
```
#### Typescript
```typescript
import { groupBy } from '@codexcentral/grouping';
...
interface Person {
name: string;
age: number;
}
const people: Person[] = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 25 }
];
console.log(groupBy(people, 'age'));
// Output:
// {
// '25': [
// { name: 'Alice', age: 25 },
// { name: 'Charlie', age: 25 }
// ],
// '30': [
// { name: 'Bob', age: 30 }
// ]
// }
```
-
partition
Partitions an array of objects into two arrays based on a predicate function.
| Parameter | Type | Description |
| --------- | -------- | ------------------------- |
| data | Array | Array of objects to group |
| predicate | Function | Predicate function |
#### Examples
```javascript
const numbers = [1, 2, 3, 4, 5];
const [even, odd] = partition(numbers, (n) => n % 2 === 0);
console.log(even); // [2, 4]
console.log(odd); // [1, 3, 5]
```
```javascript
const people = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 25 },
];
const [young, old] = partition(people, (person) => person.age < 30);
console.log(young); // [{ name: 'Alice', age: 25 }, { name: 'Charlie', age: 25 }]
console.log(old); // [{ name: 'Bob', age: 30 }]
```
```javascript
const words = ["apple", "banana", "cherry", "date"];
const [short, long] = partition(words, (word) => word.length <= 5);
console.log(short); // ['apple', 'date']
console.log(long); // ['banana', 'cherry' ]
```
-
groupItems
Groups a complex array into arrays of a specified size.
| Parameter | Type | Description |
| --------- | ------ | ------------------------- |
| data | Array | Array of objects to group |
| size | Number | Size of each group |
#### Example
```typescript
interface IProduct {
name: string;
price: number;
}
const products: IProduct[] = [
{ name: "Laptop", price: 1000 },
{ name: "Mouse", price: 20 },
{ name: "Keyboard", price: 50 },
{ name: "Monitor", price: 300 },
{ name: "USB cable", price: 5 },
];
const groupedProducts = groupItems(products, 2);
console.log(groupedProducts);
// Output:
// [
// [
// { name: "Laptop", price: 1000 },
// { name: "Mouse", price: 20 },
// ],
// [
// { name: "Keyboard", price: 50 },
// { name: "Monitor", price: 300 },
// ],
// [{ name: "USB cable", price: 5 }],
// ];
```
-
chunk
Groups a simple array into arrays of a specified size.
| Parameter | Type | Description |
| --------- | ------ | ------------------------- |
| data | Array | Array of objects to group |
| size | Number | Size of each group |
#### Example
```typescript
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const chunkedNumbers = chunk(numbers, 3);
console.log(chunkedNumbers);
// Output:
// [
// [ 1, 2, 3 ],
// [ 4, 5, 6 ],
// [ 7, 8, 9 ],
// [ 10 ]
// ]
```
#### Example
```typescript
const letters = ["a", "b", "c", "d", "e", "f", "g", "h"];
const groupedLetters = chunk(letters, 4);
console.log(groupedLetters);
// Output:
// [
// ['a', 'b', 'c', 'd'],
// ['e', 'f', 'g', 'h']
// ]
```
# Credits
This code was written by [Roberto Silva Z.](https://www.linkedin.com/in/robertosilvazuniga/)
[
](https://www.buymeacoffee.com/robertosilva)