1

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
2
  • 2
    array3[counter] is the value at index counter, not an array - hence you can't call .push on it. Commented Jan 20, 2014 at 20:22
  • "Uncaught TypeError: Cannot call method 'push' of undefined" -> array3[counter] = undefined -> no value at counter. Commented Jan 20, 2014 at 20:24

1 Answer 1

2

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

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.