js algorithm to add elements

function insert(arr, item, index) {
    
    
var arr1=arr.slice(0);
    arr1.splice(index,0,item);
    return arr1;
}

Insert picture description here

Idea:
Use slice(0) to make a new array, then intercept 0 from index, and add item to the position of index.

Guess you like

Origin blog.csdn.net/qq_37805832/article/details/115190175