简体   繁体   中英

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.


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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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