Insert Elements into a Specific Index of an Array
2021-03-04 - JavaScript
1// JS Tip of the Day
2// Insert Elements into a Specific Index of an Array
3
4const heroes = ['Batman', 'Wonder Woman'];
5
6const insertAt = (array, index, ...items) => {
7 array.splice(index, 0, ...items);
8 return array; // return the array if you want
9}
10
11console.log(insertAt(heroes, 1, 'Green Arrow', 'Superman'));
12// ['Batman', 'Green Arrow', 'Superman', 'Wonder Woman']
#JStipoftheday