PDA

View Full Version : Iterating through a multi-dimentional array, how to??



shachi
10-15-2006, 11:23 AM
Hello all,

As my title states can anyone tell me how do I iterates through a multi-dimentional array which looks sth like this:

[1,45,32,["data1","data2",34,3.4]]

and display all the data sth like this:

1;45;32;<"data1";...>

Any help would be greatly appreciated.

mburt
10-15-2006, 03:51 PM
variable[index[index]] I'm assuming.

Twey
10-15-2006, 08:38 PM
If it's only two dimensions, you can hard-code it:
for(var i = 0; i < arr.length; ++i)
if(arr[i] instanceof Array)
for(var j = 0; j < arr[i].length; ++j)
doSomething(arr[i][j]);
else doSomething(arr[i]);However, for a more robust solution, you'll need a recursive function:
function arrIter(arr) {
for(var i = 0; i < arr.length; ++i)
if(arr[i] instanceof Array)
arrIter(arr[i]);
else
doSomething(arr[i]);
}

mwinter
10-15-2006, 09:08 PM
if(arr[i] instanceof Array)

The instanceof operator will cause syntax errors on older browsers. The constructor property will work across more browsers:



function isArray(object) {
return Boolean(object) && (object.constructor == Array);
}

If the argument will never be null or undefined, the first sub-expression could be removed.

Mike

jscheuer1
10-16-2006, 04:00 AM
While we have the experts assembled here over array objects, I've often wondered if there were:

1 ) a way to iterate through the array objects that exist on a page without knowing their variable names.

2 ) to select one or more of them on the basis of their variable name matching a string.

Number one is more important to me, number two could be fudged out in other ways if number one can be done.

Twey
10-16-2006, 05:11 AM
1 ) a way to iterate through the array objects that exist on a page without knowing their variable names.Only if you know where they're stored. If they're all globals, for example, they are properties of the global object, meaning you can do:
var arrs = [];
for(var i in window)
if(window[i].constructor === Array)
arrs.push(window[i]);
2 ) to select one or more of them on the basis of their variable name matching a string.Likewise:
var arrs = [];
for(var i in window)
if(window[i].constructor === Array && i.indexOf("myArrVars") !== -1)
arrs.push(window[i]);Of course, in a well-designed script, this should never be necessary.


Alternatively, you could overwrite Array:
var tArr = function() {
return (Array.instances[Array.instances.length] = new RealArray);
};

tArr.instances = [];

RealArray = Array;

Array = tArr;This will give you a nice neat array of all instances of Array in Array.instances, but may break some functionality (the Array(x, y, z) array constructor syntax for a start; there's probably a way around this, but I'm not yet sure what it is).

jscheuer1
10-16-2006, 05:37 AM
Cool, but no good in IE - what a surprise! This was interesting:


for(var i in window)
if(window[i]&&window[i].constructor)
alert(window[i].constructor);

mwinter
10-16-2006, 02:46 PM
Alternatively, you could overwrite Array:
var tArr = function() {
return (Array.instances[Array.instances.length] = new RealArray);
};

tArr.instances = [];

RealArray = Array;

Array = tArr;This will give you a nice neat array of all instances of Array in Array.instances, but may break some functionality (the Array(x, y, z) array constructor syntax for a start; there's probably a way around this, but I'm not yet sure what it is).

The only way to directly invoke the constructor function with the new operator and an unknown set of arguments is using eval. However, one could just as easily implement the Array constructor function oneself:



function Array(value) {
var array = [],
length;

if ((length = arguments.length))
if ((length == 1) && (typeof value == 'number'))
array.length = value >>> 0;
else
for (var i = 0; i < length; ++i) array.push(arguments[i]);
Array.instances.push(array);
return array;
}
Array.instances = [];

This doesn't exactly mimic the behaviour of the constructor - if the single numeric argument form isn't a 32-bit unsigned integer, an exception should occur - but it's probably close enough (assumng it works at all: it's untested). That said, I wouldn't advise it: create a "static" method of the Array constructor function that adds this behaviour, rather than modifying the constructor itself.

Mike

Twey
10-16-2006, 04:27 PM
This was interesting:And time-consuming, I should think :)

jscheuer1
10-16-2006, 04:49 PM
And time-consuming, I should think :)

Not in IE, unfortunately. I tested in IE, FF and O. It was a rather simple page so, it didn't take too long even in FF which by far had the most responses. Opera was in between.

Doesn't anybody else learn stuff this way? I don't mean you personally Twey but, I am amazed by how many folks ask if they can do something when just trying it would give them an answer.

Twey
10-16-2006, 06:00 PM
Does JScript have a .constructor property? I was of the belief that it didn't, but assumed included IE in the statement:
The constructor property will work across more browsersAnd yes, you're right. The acronym TIAS (Try It And See) is seen far too frequently. Sometimes, of course, it is faster to just ask if something will work, rather than constructing a demo page to try it. This doesn't usually apply to asking via forum, however.

/EDIT: Heh, speaking of which... JScript does indeed support .constructor. What broke?

shachi
11-12-2006, 03:37 PM
Sorry guyz, I couldn't actually reply(coz I didn't have internet access from home and I couldn't get a computer at school library{too little computers}). Thanks Twey but can you tell me how do I use it? I tried it but couldn't get it to work(little javascript knowledge)