https://github.com/writetome51/array-insert-middle
Function that inserts item(s) into the middle of the array
https://github.com/writetome51/array-insert-middle
Last synced: 11 months ago
JSON representation
Function that inserts item(s) into the middle of the array
- Host: GitHub
- URL: https://github.com/writetome51/array-insert-middle
- Owner: writetome51
- License: mit
- Created: 2018-11-06T23:23:21.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-04-13T01:57:46.000Z (almost 7 years ago)
- Last Synced: 2025-01-02T07:26:12.512Z (about 1 year ago)
- Language: TypeScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# insertMiddle(
values,
array,
offset? = 0
): void
Inserts `values` into the middle of `array`.
If you want to change the insert position, set the optional `offset` parameter
to + or - whatever integer you want.
## Examples
```
let array = [1,2,3,4,5,6];
insertMiddle([9,10], array);
// array is now [1,2,3,9,10,4,5,6]
// By default, if array has odd number of items, values will be inserted just
// before the middle item:
let array = [1,2,3,4,5];
insertMiddle([9,10], array);
// array is now [1,2,9,10,3,4,5]
// This changes the insert position:
let array = [1,2,3,4,5];
insertMiddle([9,10], array, 1);
// array is now [1,2,3,9,10,4,5]
```
## Loading
```
// if using TypeScript:
import {insertMiddle} from '@writetome51/array-insert-middle';
// if using ES5 JavaScript:
var insertMiddle = require('@writetome51/array-insert-middle').insertMiddle;
```