最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can Array.splice() be used to create a sparse array by adding an element at an index beyond the last element of the

programmeradmin0浏览0评论

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.

Share Improve this question asked Dec 30, 2015 at 17:34 DexygenDexygen 12.6k13 gold badges86 silver badges151 bronze badges 2
  • 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
Add a ment  | 

3 Answers 3

Reset to default 5

No. The ECMAScript specification does not allow a relative start position greater than the array length. From ES2015 on Array.prototype.splice, step 7:

  1. ...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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论