1

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?

3
  • 2
    What is the practical use of this? FYI, whatever strategy you use for reading the inner value of 1, you can just assign another array to that same reference. Commented Feb 2, 2016 at 21:58
  • What is your desired output? What have you tried? Commented Feb 2, 2016 at 21:58
  • @jfriend00 it is making an organized array system with sub-arrays that makes it easier to acheive certain things Commented Feb 2, 2016 at 22:10

2 Answers 2

3

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

It says I can accept an answer in 5 min. In the meantime, I tried the code and you seem to be correct
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.