Array Extensions
v0.1.0
The Array Extension methods extend the global Array Object´s Prototype.
Usage
Import the extensions part as early as possible to make sure the global Object Prototypes are extended.
Your IDE might offer the typing without importing this init function as they are declared globally.
import { initArrayExtensions } from '@oardi/ts-utils';
initArrayExtensions();
Array Definition
The following Array operations are being executed on this array:
interface IPerson {
id: number;
name: string;
hobby: string;
}
const persons: Array<IPerson> = [
{ id: 1, name: 'John', hobby: 'Hiking' },
{ id: 2, name: 'Doe', hobby: 'Running' },
{ id: 3, name: 'Marry', hobby: 'Swimming' },
{ id: 4, name: 'Max', hobby: 'Hiking' },
{ id: 5, name: 'Maddy', hobby: 'Swimming' },
{ id: 6, name: 'Pete', hobby: 'Running' },
{ id: 7, name: 'Anna', hobby: 'Hiking' },
];
distinct
persons.map(person => person.hobby).distinct();
// ["Hiking", "Running", "Swimming"]
filterBy
persons.filterBy(person => person.name === 'John');
// [{ id: 1, name: 'John', hobby: 'Hiking' }]
first
persons.first();
// {id: 1, name: "John", hobby: "Hiking"}
groupBy
persons.groupBy(entry => entry.hobby);
// Output:
// Hiking: Array[3]
// Running: Array[2]
// Swimming: Array[2]
orderBy
persons.orderBy(entry => entry.hobby);
removeBy
persons.removeBy(person => person.id, 1); Previous: General
← Get startedNext: Extensions
Date Extension →