https://github.com/writetome51/array-get-adjacent-at
Function returns chosen number of adjacent items in array beginning at chosen index
https://github.com/writetome51/array-get-adjacent-at
array-function array-manipulations get items javascript typescript
Last synced: about 1 year ago
JSON representation
Function returns chosen number of adjacent items in array beginning at chosen index
- Host: GitHub
- URL: https://github.com/writetome51/array-get-adjacent-at
- Owner: writetome51
- License: mit
- Created: 2018-10-15T03:57:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-03T04:02:59.000Z (over 4 years ago)
- Last Synced: 2025-02-02T10:19:27.044Z (about 1 year ago)
- Topics: array-function, array-manipulations, get, items, javascript, typescript
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# getAdjacentAt\(
startingIndex: number,
howMany: number,
array: T[]
): T[]
Beginning at `startingIndex`, returns `howMany` adjacent items from `array`.
Does not modify `array`. `startingIndex` can be negative or positive.
Intended as a replacement of Array.prototype.slice() . It strictly validates args.
## Examples
```js
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
getAdjacentAt(0, 3, arr);
// --> [1,2,3]
getAdjacentAt(2, 5, arr);
// --> [3,4,5,6,7]
// You can pass a negative index:
getAdjacentAt(-3, 2, arr);
// --> [8, 9]
getAdjacentAt(-2, 3, arr); // Requesting one too may items
//Error: 'the array does not have enough items to fulfill your request'
```
## Installation
`npm i @writetome51/array-get-adjacent-at`
## Loading
```js
import { getAdjacentAt } from '@writetome51/array-get-adjacent-at';
```