Skip to content

Commit 08bd942

Browse files
authored
Update insertAt.js
1 parent a12b69a commit 08bd942

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

js-coding-technique/insertAt.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
// Insert an item into an array at a specific index (Immutable insertion)
22
/**
3-
*
4-
* @param {Array} arr
5-
* @param {Int} index
6-
* @param {*} values
3+
*
4+
* @param {Array} array
5+
* @param {Int} index
6+
* @param {*} items
77
* @returns arr {New Array}
8-
*
9-
* @demo let bar = insertAt(arr, 2, 'a', 'b')
8+
*
9+
* @demo let bar = insertAt(array, 2, 'a', 'b')
1010
*/
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-
};
11+
export const insertAt = (array, index, ...items) => [...array.slice(0, index), ...items, ...array.slice(index)];

0 commit comments

Comments
 (0)