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?
stark
2,2562 gold badges24 silver badges35 bronze badges
2 Answers 2
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]);
answered Feb 2, 2016 at 21:58
madox2
52.3k21 gold badges106 silver badges101 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
guyys
It says I can accept an answer in 5 min. In the meantime, I tried the code and you seem to be correct
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);
answered Feb 2, 2016 at 22:10
Viktor Kukurba
1,3709 silver badges14 bronze badges
Comments
lang-js
1, you can just assign another array to that same reference.