@codexcentral/grouping
v1.2.0
Published
Functions to group data by a key or multiple keys.
Downloads
36
Maintainers
Readme
grouping
Functions to group data by a key or multiple keys.
Installation
npm install @codexcentral/grouping
Importing
Javascript
const { groupBy } = require("@codexcentral/grouping");
Typescript
import { groupBy } from "@codexcentral/grouping";
Functions
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
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
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 } // ] // }
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
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]
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 }]
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' ]
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
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 }], // ];
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
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
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.