How to extract array in other arrays with for loop

Hi guys,

I’ve an array looks like this:


How do I get each array from the two big ones? I try a for loop but I’m unable to get them.

Thank you,

Here’s one way to do it:

mainArray.forEach(function (subArray) {
    subArray.forEach(function (deeperSubArray) {
        // each of the deeperSubArrays will be for example [0,1,2], or [5,4 3]
1 Like

Does it have to be a for loop? Because you already have built-in methods for arrays like splice and push:

var array = [ 

var array1=[];
var array2=[];
var array3=[];
var array4=[];
var array5=[];
var array6=[];

document.write(array1 + "<br>" + array2 + "<br>" + array3 + "<br>" + array4 + "<br>" array5 + "<br>" + array6)

This post has been reformatted by enclosing the code block in 3 backticks
on their own lines.

Thank you, it works

Hi v15ko,

It’s not necessary for loop. I can’t think of anything else, so I try for loop. :smile:

An array in Javascript has a “length” property, holding the number of its elements. For example if your array is defined as

arr = [

“arr” is a 3 dimensional array containgng 2 2-dimentional arrays inside. Let’s call each of them “row”:

Access each row using:

    for(i=0; i<arr.length; i++){
      row = arr[i]; // An array of vectors


methods that allow you to process it without ever needing a loop.

The forEach method is the most generic but there are several others that allow you to easily perform just about any array processing with a single call instead of a loop.

Paul posted the code in post 2 of this thread that allows each of the innermost arrays to be processed directly without needing to loop over the outer two levels of array.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.