简体   繁体   English

将数组推入嵌套数组JavaScript

[英]Push array into Nested array javascript

I have some code like this: 我有一些这样的代码:

var array=[[[[[[1],2],3],4],5],6]

I know how to retrieve the innermost array, but I don't know how to push a new array [0] into the innermost array. 我知道如何检索最里面的数组,但是我不知道如何将新数组[0]推入最里面的数组。


Is this possible? 这可能吗?

I suppose you want to add new inner-most array on the first position. 我想您想在第一个位置添加新的最里面的数组。 You can use unshift function: 您可以使用取消移位功能:

array[0][0][0][0][0].unshift([0]);

Try this one 试试这个

var array=[[[[[[1],2],3],4],5],6];
function pushNew(arr, newElement){
  if(arr[0].length === 2){
    pushNew(arr[0], newElement);
  } else {
    arr[0].unshift([newElement]);
  }
}
pushNew(array,0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM