https://github.com/writetome51/array-remove-by-indexes
Removes items from array, identified by indexes
https://github.com/writetome51/array-remove-by-indexes
array-manipulations items javascript remove typescript
Last synced: 3 months ago
JSON representation
Removes items from array, identified by indexes
- Host: GitHub
- URL: https://github.com/writetome51/array-remove-by-indexes
- Owner: writetome51
- License: mit
- Created: 2018-11-02T02:14:05.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-10-26T23:32:23.000Z (about 4 years ago)
- Last Synced: 2025-02-03T08:47:52.277Z (11 months ago)
- Topics: array-manipulations, items, javascript, remove, typescript
- Language: JavaScript
- Size: 34.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# removeByIndexes\(
indexes: number[],
array: T[],
useRemovedItem?: (item: T) => void
): void
Removes items, identified by `indexes`, from `array`.
Negative indexes not allowed.
Optional callback `useRemovedItem()` lets you collect each removed item
for further manipulation.
The algorithm first sorts `indexes` in ascending order. Then the items of
`array` are removed in descending index order, i.e., item with index 0 would
be removed last.
## Examples
```js
let arr = [10,20,30,40,50,60];
removeByIndexes([3, 1], arr);
// arr is now [10, 30, 50, 60].
arr = ['he', 'llo', 'zz', 'gg', 'cc'];
let removed = [];
removeByIndexes([4, 0], arr, (item) => removed.push(item));
// removed is ['cc', 'he'] (remember, elements are removed in descending index order).
// arr is ['llo', 'zz', 'gg'].
arr = [10,20,30,40,50,60];
removed = [];
removeByIndexes([1, 3, 5], arr, (item) => removed.push(item));
// removed is [60, 40, 20]
// arr is [10, 30, 50].
// It can work with duplicate indexes, though the result may not be
// what you wanted:
arr = ['he', 'llo', 'zz', 'gg', 'cc', 'aa'];
removeByIndexes([2, 3, 4, 4], arr);
// arr is ['he', 'llo']
arr = ['he', 'llo', 'zz', 'gg', 'cc', 'aa'];
removeByIndexes([1, 1, 1], arr);
// arr is ['he', 'cc', 'aa']
// Don't pass string numbers. You'll get confusing errors or unexpected results.
removeByIndexes(['1'], arr);
// Error: "the array does not have enough items to fulfill your request"
```
## Installation
`npm i @writetome51/array-remove-by-indexes`
## Loading
```js
import {removeByIndexes} from '@writetome51/array-remove-by-indexes';
```