https://github.com/writetome51/array-get-and-remove-adjacent-at
Function removes and returns adjacent array items beginning at specified index
https://github.com/writetome51/array-get-and-remove-adjacent-at
array-manipulations javascript typescript
Last synced: 11 months ago
JSON representation
Function removes and returns adjacent array items beginning at specified index
- Host: GitHub
- URL: https://github.com/writetome51/array-get-and-remove-adjacent-at
- Owner: writetome51
- License: mit
- Created: 2018-11-05T23:08:03.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-02T05:52:47.000Z (about 4 years ago)
- Last Synced: 2025-01-02T07:26:05.822Z (about 1 year ago)
- Topics: array-manipulations, javascript, typescript
- Language: JavaScript
- Size: 19.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getAndRemoveAdjacentAt\(
startingIndex: number,
howMany: number,
array: T[]
): T[]
Beginning at `startingIndex`, it removes and returns `howMany` adjacent items from `array`.
`startingIndex` can be positive or negative.
Intended as a replacement for Array.prototype.splice() when removing items from `array`.
This does strict validation for all 3 parameters.
## Examples
```js
let arr = [1,2,3,4,5,6];
getAndRemoveAdjacentAt(1, 3, arr);
// --> [2,3,4]
// arr is now [1,5,6]
arr = [1, 2, 3, 4, 5, 6, 7, 8];
getAndRemoveAdjacentAt(-4, 2, arr);
// --> [5,6]
// arr is now [1,2,3,4,7,8].
arr = [1, 2, 3, 4, 5, 6];
getAndRemoveAdjacentAt(-2, 3, arr); // requesting 1 too many items.
// Error: 'the array does not have enough items to fulfill your request'
getAndRemoveAdjacentAt(0, 1, []); // index 0 doesn't exist on empty array.
// Error: 'The entered index is not valid. Whether positive or negative,
// it exceeds the index range of the array.'
```
## Installation
`npm i @writetome51/array-get-and-remove-adjacent-at`
## Loading
```js
import {getAndRemoveAdjacentAt}
from '@writetome51/array-get-and-remove-adjacent-at';
```