I guess I don’t understand what you mean by “work around” - the method is doing exactly what is expected to do as defined. The problem is that in the original, you are confusing what the method does to the original array and what it returns. When you say:
var arr1 = arr.splice(0,1,9);
you are not assigning arr into arr1. The right hand of the assignment is evaluating as the return of that splice this is a very important distinction.
If you want arr1 to be the same as arr after that operation, then you could try something like:
arr.splice(0,1,9);
var arr1 = arr;
But that might not give the results you want. In that case you are setting arr1 to point to the array *arr*. So there is just one array that now has two names, pointing to the same data. Consider the following:
var arr = [1, 2, 3, 4, 5];
var arr1 = arr;
console.log(“before splice, arr == " + arr.join(”,"));
console.log(“before splice, arr1 == " + arr1.join(”,"));
// before splice, arr = 1,2,3,4,5
// before splice, arr1 = 1,2,3,4,5
arr.splice(0, 1, “apple”);
console.log(“after splice, arr == " + arr.join(”,"));
console.log(“after splice, arr1 == " + arr1.join(”,"));
// after splice, arr = apple,2,3,4,5
// after splice, arr1 = apple,2,3,4,5
Strange, huh? But it makes sense if you understand the difference between primitive and reference types. When you have a variable for an array (assuming this is the same as it was in C), you really have an address for that data structure. When you copy that variable, you're really just copying the the address. This should be true for all non-primitive types.
If you want a copy of the address, then that's fine. But if you want a separate copy so that manipulation of one won't affect the other, then to copy the array, you can do `var arr1 = arr.slice();` That will create a completely new and independent copy.
var arr = [1, 2, 3, 4, 5];
var arr1 = arr.slice();
console.log(“before slice, arr == " + arr.join(”,"));
console.log(“before slice, arr1 == " + arr1.join(”,"));
// before splice, arr = 1,2,3,4,5
// before splice, arr1 = 1,2,3,4,5
arr.splice(0, 1, “apple”);
console.log(“after slice, arr == " + arr.join(”,"));
console.log(“after slice, arr1 == " + arr1.join(”,"));
// after splice, arr = apple,2,3,4,5
// after splice, arr1 = 1,2,3,4,5
OK, it's a little weird. But everything is until you get used to it.