简体   繁体   中英

How to insert array into specific index of array?

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/

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?

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