Can Array.splice()
be used to create a sparse array by adding an element at an index beyond the last element of the array?" I need this because in some situations I just want to push onto the array, but in other situations I need to splice into the array. But trying to use splice to make the array sparse did not work, though in my particular situation I was able to implement some code to test whether to use splice, or just assign an array element at an index beyond the array's length.
Can Array.splice()
be used to create a sparse array by adding an element at an index beyond the last element of the array?" I need this because in some situations I just want to push onto the array, but in other situations I need to splice into the array. But trying to use splice to make the array sparse did not work, though in my particular situation I was able to implement some code to test whether to use splice, or just assign an array element at an index beyond the array's length.
- 1 I thought this question was worth a self-answered Q&A as I unwittingly attempted to do so and discovered the answer, but there are no easily identifiable google results that are on point if you do a search. – Dexygen Commented Dec 30, 2015 at 17:34
-
1
Array.prototype.splice(start, deleteCount, item1...)
-> Parameters: start: Index at which to start changing the array. If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end. – Andreas Commented Dec 30, 2015 at 17:39
3 Answers
Reset to default 5No. The ECMAScript specification does not allow a relative start position greater than the array length. From ES2015 on Array.prototype.splice
, step 7:
- ...let actualStart be min(relativeStart, len).
The variable actualStart is what's actually used for the splice
algorithm. It's produced by the minimum of relativeStart (the first argument to the function) and len (the length
of the array). If len is less than relativeStart, then the splice
operation will use len instead the actual argument provided.
In practical terms, this means that you can only append values onto the end of arrays. You cannot use splice
to position a new element past the length
index.
It should be noted the length of the array is not necessarily the index of the last item in the array plus 1. It can be greater.
Then, you can't use splice
to insert elements beyond the length of the array, but if you make sure the length is large enough, you can insert beyond the last index plus 1.
var arrSplice = ['what', 'ever'];
arrSplice.length = 10; // Increase the capacity
arrSplice.splice(10, 0, 'foobar'); // Now you can insert items sparsely
console.log(arrSplice.length); // 10
console.log(arrSplice[arrSplice.length - 1]); // foobar
Array.splice()
cannot be used to create sparse arrays. Instead, if the index argument passed to Array.splice()
is beyond the length of the array, it seems that the element just gets appended to the array as if Array.push()
had been used.
/* How you might normally create a sparse array */
var arrNoSplice = ['foo', 'bar'];
arrNoSplice[10] = 'baz';
console.log(arrNoSplice.length); // 11
/* Demonstrates that you cannot use splice to create a sparse array */
var arrSplice = ['what', 'ever'];
arrSplice.splice(10, 0, 'foobar');
console.log(arrSplice.length); // 3
console.log(arrSplice[arrSplice.length - 1]); // foobar