PDA

View Full Version : Testing Array


JlynnMc10
02-11-2003, 04:23 PM
If I created an array and added data to it in a .js, is there a way I could print out the contents of the array so that I can check to make sure the data is getting manipulated correctly.

Danne
02-11-2003, 04:32 PM
You can loop through it and assign it to a string if the array has more than one dimension...



function _printArr(arr)
{
var i,str="";
for(i=0;i<arr.length;i++)
str+="arr["+i+"]: "+arr[i]+"\n"
alert(str);
}



If it has only one dimension, you can do something like...


function _printArr(arr)
{
alert(arr.join("\n"));
}




If you have a multidimensional array you could check the "typeof" each item. It is "object" you could assume it is another array..

JlynnMc10
02-11-2003, 05:20 PM
Would this be the correct way to call the print function in my .js library file? And if so, then how would I get the array to show up on my website?


var data;
for(var i=0;i<data.length;i++){
data[i] = i;
}
_printArr(data);

JlynnMc10
02-11-2003, 06:00 PM
How would I call the function so that the contents of the array printed on my webpage.

Tails
02-11-2003, 08:15 PM
Try this to type the value of each array and it's type:

for (i=0; i<arr.length; i++)
{
document.write(arr[i]+"&nbsp;"+ typeof(arr[i])+"<br />")
}

JlynnMc10
02-11-2003, 08:41 PM
for (var i=0; i<tree.length; i++){
document.write(tree[i]+" "+ typeof(tree[i])+"<br />");
}

This was my output:

, object
,, object
object

What does this mean? My guess is that my array contains three other arrays??? If the objects are arrays is there a way I could see the data in those arrays?

cheesebagpipe
02-12-2003, 01:55 AM
This is pretty rough; but it might help you. New Array method:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
</head>
<body>
<script type="text/javascript" language="javascript">

/////////////////////////////////////////////////////////////////////////////
Array.prototype.print = function(asHTML) {
var el, sMem, i = 0, typeofobj, lfeed = ((asHTML) ? '<br />' : '\n');
var sOut = 'Array Elements' + lfeed + ':::::::::::::::::::::' + lfeed;
for (var i=0; i<this.length; ++i) {
el = this[i];
if (el == null) {
sOut += '[' + i + '] null' + lfeed;
continue;
}
typeofobj =
typeof el != 'object' ? typeof el + ' primitive' :
el.constructor == Array ? 'array object' :
el.constructor == Number ? 'number object' :
el.constructor == String ? 'string object' :
el.constructor == Boolean ? 'boolean object' :
'object';
if (typeofobj == 'object') {
sMem = '{ ';
for (prop in this[i]) sMem += prop + ' : ' + this[i][prop] + ' (' + typeof this[i][prop] + ') , ';
sMem = sMem.substring(0,sMem.length-3) + ' }';
}
else sMem = this[i];
if (typeofobj == 'array object') sMem = '[ ' + sMem + ' ]';
sOut += '[' + i + '] ' + typeofobj + ': ' + sMem + lfeed;
}
return ((asHTML) ? '<b>' : '') + sOut + ':::::::::::::::::::::' + ((asHTML) ? '</b>' : '');
}
/////////////////////////////////////////////////////////////////////////////

var x = [null,98.6,'poopy',new String('poopy'),{mama:'mia',rpm:33.3},['*',{},9],false,new Number(99)];

document.write("<hr />");
document.write("<font color=indigo>[ null , 98.6 , 'poopy' , new String('poopy') , {mama:'mia',rpm:33.3} , ['*',{},9] , false , new Number(99) ]</font>");
document.write("<hr /><br />");

document.write(x.print(true));

</script>
<br /><br />
<a href="javascript:void alert(x.print(false))">show me</a>
</body>
</html>


Doesn't quite parse those nests...working on some recursion.