Forum Moderators: open
The question I have is that I'm not always going to be sure how many dimensions the array will have.
Is there any way to get the count of dimensions. IE,
arr[2] // the #s are just fillers
arr.dimensions() == 1
arr[0][1]
arr.dimensions() == 2
arr[2][0][4]
arr.dimensions() == 3
arr[2][1][3][3][0]
arr.dimensions() == 5
Anything like that? Or a way to find out?
-------------------------------------
And then, is there a way to find the length of the dimension. So for example:
1 2 3
4 5 6
7 8 9
a b c
arr[0][0] = 1; arr[0][1] = 2; arr[0][2] = 3;
arr[1][0] = 4; arr[1][1] = 5; arr[1][2] = 6;
arr[2][0] = 7; arr[2][1] = 8; arr[2][2] = 9;
arr[3][0] = a; arr[3][1] = b; arr[3][2] = c;
arr.length == 4
arr[0].length == 3
arr[0][0].length == undefined
Something like that?
-------------------------------------
Thx in advance,
vol7ron
<script type="text/javascript">
var foo = new Array(new Array(new Array(new Array(new Array(new Array(new Array(new Array())))))));
var depth = 1;
var x = foo[0];
var str = "";
while(x > -1) {
str = "foo[0]";
for(i = 0; i < depth; i++) {
str = str + "[0]";
}
eval("x = " + str);
depth = depth + 1;
}
document.write(depth);
</script>
Obviously you would not need to generate the
foo array, but could simply pass it along.
When you do this:
arr[0][1]
You're accessing the item in arr[0] which is another array, and then you're accessing the item in that array with index 1.
For example:
arr[0] = [0,1,2];
arr[1] = [0,1,2,3];
arr[2] = [0];
or:
arr[0][0] = 0;
arr[0][1] = 1;
arr[0][2] = 2;
arr[1][0] = 0;
arr[1][1] = 1;
arr[1][2] = 2;
arr[1][3] = 3;
arr[2][0] = 0;
If you try to access arr[2][1], you will get undefined because that index is not included in the sub array.
With that said, you could write your own function to calculate the "simulated" dimensions.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Untitled</title>
</head>
<body>
<script type="text/javascript">
var arr = []; // A one dimensional array
arr[0] = 0; // Still only 1 dimension
arr[1] = [0,1,2,3]; // A 2nd dimension has been added
arr[2] = [0]; // Still only 2 dimensions
arr[3] = [['a',[]],['c','d']]; // A 3rd and 4th dimension added
function getDimensions(a) {
var i, depth = 0, d, dx = 0;
if (typeof a === 'object' && undefined !== a.length) {
// this is an array
depth = 1;
for (i = 0; i < a.length; i++) {
d = getDimensions(a[i]);
dx = (d > dx? d : dx);
}
depth += dx;
}
return depth;
}
alert("arr has " + getDimensions(arr) + " dimension(s)");
</script>
</body>
</html>
Note, JavaScript stores arrays as type 'object'. This method is checking for an object with a property of 'length', so if you were to pass in an object that had a property named length, this would treat that as an array. Here's an example:
arr[0] = {"name": "foo", "length": "3"};
There may be a better way to check for arrays.
- I haven't looked at your function yet, I'll do it in a little.
- The problem with the array.length property is that it's not read only. You can create an array by changing the length. Example:
arr[0] = "blah";
arr.length == 1 // only one node in array
arr.length = 100 // set new length
arr.length == 100 // arr[0..100] VB-like syntax