Skip to content

Commit c094155

Browse files
authored
Create insertAt.js
1 parent 1650089 commit c094155

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

js-coding-technique/insertAt.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Insert an item into an array at a specific index (Immutable insertion)
2+
/**
3+
*
4+
* @param {Array} arr
5+
* @param {Int} index
6+
* @param {*} values
7+
* @returns arr {New Array}
8+
*
9+
* @demo let bar = insertAt(arr, 2, 'a', 'b')
10+
*/
11+
export const insertAt = (arr, index) => {
12+
const items = Array.prototype.slice.call(arguments, 2);
13+
14+
return [].concat(arr.slice(0, index), items, arr.slice(index));
15+
};

0 commit comments

Comments
 (0)