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
- Host: GitHub
- URL: https://github.com/writetome51/array-get-and-remove-by-test
- Owner: writetome51
- License: mit
- Created: 2018-10-27T01:51:23.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-15T06:51:47.000Z (about 4 years ago)
- Last Synced: 2025-07-01T07:02:39.579Z (7 months ago)
- Topics: array-manipulations, filter, javascript, typescript
- Language: JavaScript
- Homepage:
- Size: 52.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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';
```