An open API service indexing awesome lists of open source software.

https://github.com/writetome51/array-get-and-remove-by-test

Receives a test function and both removes and returns array items that pass test
https://github.com/writetome51/array-get-and-remove-by-test

array-manipulations filter javascript typescript

Last synced: 5 months ago
JSON representation

Receives a test function and both removes and returns array items that pass test

Awesome Lists containing this project

README

          

# getAndRemoveByTest\(
      test: (
            value: T, index?: number, array?: T[]
      ) => boolean,
      array: T[],
      getValue?: (
            value: T, index?: number, array?: T[]
      ) => X
): X[]

Removes and returns items in `array` that pass `test`.
Includes optional callback `getValue()`, which lets you customize what value to get from an element
that passes `test`.

## Examples
```js
let arr = ['hello', '??', 2, 'h', 1, 5, 'h', 'mertyujkl;', 20];
getAndRemoveByTest((item) => (item[0] === 'h'), arr);
// --> ['hello', 'h', 'h']
// arr is now ['??', 2, 1, 5, 'mertyujkl;', 20];

arr = [true, 10, false, 2, 'h', 1, true];
getAndRemoveByTest(
(item) => typeof item === 'boolean',
arr,
(value, index) => ( {value, index} )
);
/**********
Returns:
[
{ value: true, index: 0 },
{ value: false, index: 2 },
{ value: true, index: 6 }
]

arr is now [10, 2, 'h', 1];
**********/
```

## Installation
```bash
npm i @writetome51/array-get-and-remove-by-test
```

## Loading
```js
import {getAndRemoveByTest} from '@writetome51/array-get-and-remove-by-test';
```