Is there any way to make the splice()
non-destructive?
What I need to do is to retain the arr2
as it is after I call the function.
function foo(arr1, arr2, n) {
let result = arr2;
result.splice(n, 0, ...arr1.slice(0, arr1.length));
console.log(arr2);
}
foo(["I'm", "not", "done"], ["sorry", "yet"], 1);
Although it returns sorry,I'm,not,done,yet
which is what I want but the elements of arr2
shouldn't change.