Skip to content

Function that inserts new items at a specified index in the array

License

Notifications You must be signed in to change notification settings

writetome51/array-insert-at

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

insertAt<T>(
      index,
      values: T[],
      array: T[]
): void

At index, values are inserted in array.
index can be negative or positive. If positive, existing items beginning at that index
will be pushed to the right to make room. If negative, existing items ending at that
index will be pushed to the left to make room. No items get removed.

Examples

let arr = [1,2,3,4];   
insertAt(2, [7, 8], arr);  
// arr is now [1,2,7,8,3,4]

let arr = [1,2,3,4];   
insertAt(-1, [7, 8], arr);  
// arr is now [1,2,3,4,7,8]

let arr = [1,2,3,4];   
insertAt(-4, [7, 8], arr);  
// arr is now [1,7,8,2,3,4]

Installation

npm i @writetome51/array-insert-at

Loading

import { insertAt } from '@writetome51/array-insert-at';

About

Function that inserts new items at a specified index in the array

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published