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

Using splice with nested arrays in Javascript - Stack Overflow

programmeradmin2浏览0评论

I have an array in which all the elements are also arrays (of integers), called mainArray. I am trying to use splice() to add and remove its elements (subarrays).

Initially mainArray has a single element (an Array with one integer), I wish to delete it and add 3 new subarrays to mainArray, these are defined in arraysToAdd.

mainArray = new Array(new Array(1));
arraysToAdd = new Array(new Array(1,2), new Array(1,4), new Array(1,7));

alert(arraysToAdd.length); // Returns: 3: as expected

mainArray.splice(0,1,arraysToAdd);

alert(mainArray.length); // Returns: 1: I want this to be 3

I expect the length of mainArray at the end to be 3 (as it should contain 3 subarrays), but it seems splice() is flattening arraysToAdd and so mainArray ends up just being an array of integers.

What am I missing?

I have an array in which all the elements are also arrays (of integers), called mainArray. I am trying to use splice() to add and remove its elements (subarrays).

Initially mainArray has a single element (an Array with one integer), I wish to delete it and add 3 new subarrays to mainArray, these are defined in arraysToAdd.

mainArray = new Array(new Array(1));
arraysToAdd = new Array(new Array(1,2), new Array(1,4), new Array(1,7));

alert(arraysToAdd.length); // Returns: 3: as expected

mainArray.splice(0,1,arraysToAdd);

alert(mainArray.length); // Returns: 1: I want this to be 3

I expect the length of mainArray at the end to be 3 (as it should contain 3 subarrays), but it seems splice() is flattening arraysToAdd and so mainArray ends up just being an array of integers.

What am I missing?

Share Improve this question asked Feb 24, 2014 at 0:42 jacobianismjacobianism 2261 gold badge3 silver badges12 bronze badges 4
  • 1 console.log(mainArray) – zerkms Commented Feb 24, 2014 at 0:44
  • "but it seems splice() is flattening arraysToAdd" Uhm, how can the length of mainArray be 1 if the other arrays is flattened into mainArray? If mainArray was an array of integers, wouldn't the length be 6? – Felix Kling Commented Feb 24, 2014 at 0:46
  • 2 You do know you can just type var mainArray = [[1]];, right? – StackSlave Commented Feb 24, 2014 at 0:51
  • @PHPglue brings up a good point. In fact, your mainArray worked because you didn't use the value, but if you had tried to use it, or if you created it with a greater number, you'd get a result that you may not expect. Like if you did new Array(3), you now have an Array with .length === 3, and no actual defined members. – cookie monster Commented Feb 24, 2014 at 1:58
Add a ment  | 

2 Answers 2

Reset to default 7

What you're missing is that you're adding an Array of Arrays to your Array of Arrays. You want to add each individual Array instead.

You can use .apply() to do this:

mainArray.splice.apply(mainArray, [0,1].concat(arraysToAdd));

So the 0 and 1 arguments you passed are joined with your arraysToAdd to form the arguments that you're going to pass to .splice() via .apply().

Demo: http://jsfiddle/QLwLA/


Without .apply(), you would have needed to add them individually, like this:

mainArray.splice(0, 1, arraysToAdd[0], arraysToAdd[1], arraysToAdd[2]);

Demo: http://jsfiddle/QLwLA/1/

try this:

mainArray.splice(0, 1, ...arraysToAdd)
发布评论

评论列表(0)

  1. 暂无评论