https://github.com/writetome51/array-remove-by-test
Function that removes items from the array that match the criteria of the test function
https://github.com/writetome51/array-remove-by-test
array array-manipulations filter items javascript remove typescript
Last synced: about 1 year ago
JSON representation
Function that removes items from the array that match the criteria of the test function
- Host: GitHub
- URL: https://github.com/writetome51/array-remove-by-test
- Owner: writetome51
- License: mit
- Created: 2018-10-01T04:39:06.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-09-09T20:53:30.000Z (over 5 years ago)
- Last Synced: 2025-02-03T12:53:18.342Z (about 1 year ago)
- Topics: array, array-manipulations, filter, items, javascript, remove, typescript
- Language: TypeScript
- Size: 23.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# removeByTest(
test: (value, index?, array?) => boolean,
array,
useRemovedItem?: (
value, index?, array?
) => void
): void
Removes any item in `array` that passes a `test`.
If you use the optional callback `useRemovedItem()`, remember that this function
removes items in descending index-order, i.e., item with index 0 is removed last.
## Example
```js
let arr = [1,2,3,4,5,6,7,8,9,10];
removeByTest((item) => (item % 2) === 0, arr);
console.log(arr);
// [1,3,5,7,9]
arr = [ [10, 2, 3], [2, 3, 4], 1, 6, false, [10, 20] ];
removeByTest(
(item) => (Array.isArray(item) && item[0] === 10),
arr
);
console.log(arr);
// [ [2, 3, 4], 1, 6, false ]
// Collect the indexes of removed items:
arr = [10, 1, 20, 2, 30, 3, 40, 4];
let found = [];
removeByTest(
(value) => value >= 10,
arr,
(value, index) => found.push(index)
);
console.log(found);
// [ 6, 4, 2, 0 ]
// (items removed in descending index-order)
```
## Installation
```bash
npm i @writetome51/array-remove-by-test
```
## Loading
```js
import {removeByTest} from '@writetome51/array-remove-by-test';
```