Hi guys,
I’ve an array looks like this:
[
[[0,1,2],[3,4,5],[6,7,8]],
[[8,7,6],[5,4,3],[2,1,0]]
]
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,
Hi guys,
I’ve an array looks like this:
[
[[0,1,2],[3,4,5],[6,7,8]],
[[8,7,6],[5,4,3],[2,1,0]]
]
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]
});
});
Does it have to be a for loop? Because you already have built-in methods for arrays like splice and push:
var array = [
[0,1,2],[3,4,5],[6,7,8],[8,7,6],[5,4,3],[2,1,0]
]
var array1=[];
var array2=[];
var array3=[];
var array4=[];
var array5=[];
var array6=[];
array1.push(array.slice(0,1))
array2.push(array.slice(1,2))
array3.push(array.slice(2,3))
array4.push(array.slice(3,4))
array5.push(array.slice(4,5))
array6.push(array.slice(5,6))
document.write(array1 + "<br>" + array2 + "<br>" + array3 + "<br>" + array4 + "<br>" array5 + "<br>" + array6)
EDIT
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.
An array in Javascript has a “length” property, holding the number of its elements. For example if your array is defined as
arr = [
[[0,1,2],[3,4,5],[6,7,8]],
[[8,7,6],[5,4,3],[2,1,0]]
];
“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.