I have tried the following but they don't work.
I want a result like [[[arr1],[arr2],[arr3]],[[arr4],[arr5],[arr6]]].
array3[counter].push(array2[e+f]);
//Uncaught TypeError: Cannot call method 'push' of undefined
array3.splice(counter,0,array2[e+f]);
//[Array[2], Array[3], Array[4], Array[4], Array[2], Array[3]]
Here is my source code if it helps: http://jsfiddle.net/yUXPz/
asked Jan 20, 2014 at 20:20
hexicle
2,1972 gold badges25 silver badges32 bronze badges
1 Answer 1
If you're trying to add an array to array3 at the specified counter location then use this:
array3[counter] = [];
array3[counter].push(array2[e+f]);
Here is the output I got:
[
[
[
7,
8,
9
]
],
[
[
3,
3,
3,
4
]
]
]
Is that what you're looking to get?
answered Jan 20, 2014 at 21:10
LameCoder
1,2978 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
hexicle
I needed the array to look like this
[[[arr1],[arr2],[arr3]],[[arr4],[arr5],[arr6]]] i.e. 3 arrays in every array, because my code was just to help me solve this HackerRank problem: hackerrank.com/challenges/two-arrays., and I needed that array structure for my algorithm.lang-js
array3[counter]is the value at index counter, not an array - hence you can't call.pushon it.