Add element (add at specified location)
Method 1: Copy the first 0~index elements first, insert the item element, and then splice the elements after index
function insert(arr, item, index) {
let newArr = arr.slice(0, index)
newArr.push(item)
newArr = newArr.concat(arr.slice(index))
return newArr
}
Method 2: Insert using splice method (higher efficiency)
function insert(arr, item, index) {
let newArr = arr.slice(0)
newArr.splice(index,0, item)
return newArr
}
Method 3: push.apply+splice
function insert(arr, item, index) {
let newArr = [];
[].push.apply(newArr,arr);
newArr.splice(index,0, item);
return newArr
}