PDA

View Full Version : Dynamic Arrays



wesleytabaka
11-04-2007, 06:44 PM
Does anyone know a way to make a javascript function that will print an array in a similar way to this:

function list(field){
var nissan=new Array();
nissan[0]="Quest";
nissan[1]="Versa";
nissan[2]="Altima";

var mitsubishi=new Array();
mitsubishi[0]="Eclipse";
mitsubishi[1]="Lancer";
mitsubishi[2]="Outlander";

for(x=0; x<field.length; x++){
document.write(field[x] + "<br />");
}
}

If I were to run the function list("nissan"), It would, in theory, write:
Quest
Versa
Altima

But, it doesn't. Instead it writes:
n
i
s
s
a
n

It thinks the field parameter is a string, which is what it is. But I want it to behave as a variable name, to be able to refer to the nissan and mitsubishi arrays. How can I make the parameter refer to those arrays instead of act as a string?

Trinithis
11-04-2007, 06:49 PM
var nissan=new Array();
nissan[0]="Quest";
nissan[1]="Versa";
nissan[2]="Altima";

var mitsubishi=new Array();
mitsubishi[0]="Eclipse";
mitsubishi[1]="Lancer";
mitsubishi[2]="Outlander";

function list(field) {
for(x=0; x<field.length; x++) document.write(field[x] + "<br />");
}


Or better yet



var nissan = ["Quest", "Versa", "Altima"];

var mitsubishi = ["Eclipse", "Lancer", "Outlander"];

function list(field) {
for(x=0; x<field.length; x++) document.write(field[x] + "<br />");
}


If you want to define the arrays inside the function, then you have to use a control statement



function list(field) {
field = field.toLowerCase();
var nissan = ["Quest", "Versa", "Altima"];
var mitsubishi = ["Eclipse", "Lancer", "Outlander"];
switch(field) {
case "nissan":
field = nissan;
break;
case "mitsubishi":
field = mitsubishi;
break;
default:
field = ["Brand not found"];
}
for(x=0; x<field.length; x++) document.write(field[x] + "<br />");
}


The first two don't take a string, but the var name. The last takes a string.

wesleytabaka
11-04-2007, 07:59 PM
Terrific! Thanks so much! :D I didn't think the solution would be so simple. :)

jscheuer1
11-04-2007, 08:12 PM
How 'bout:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
list.nissan=["Quest","Versa","Altima"];
list.mitsubishi=["Eclipse","Lancer","Outlander"];
function list(field){
for(var x=0; x<list[field].length; x++)
document.write(list[field][x] + "<br>");
}
</script>
</head>
<body>
<script type="text/javascript">
list('nissan');
</script>
</body>
</html>

Twey
11-04-2007, 08:34 PM
Academically:
<script type="text/javascript">
var listCars = (function(listsObj) {
return function(field) {
for(var i = 0, a = listsObj[field] || [], n = a.length; i < n; ++i)
document.write(a[i] + "<br>");
};
})({
mitsubishi: ["Eclipse", "Lancer", "Outlander"],
nissan: ["Quest", "Versa", "Altima"]
});
</script>However, note that document.write() is no longer a very good idea, and this is an abuse of <br>. Instead, you could have the function return a list element:
<script type="text/javascript">
var listCars = (function(listsObj) {
return function(field) {
for(var i = 0, r = document.createElement("ul"), a = listsObj[field] || [], n = a.length; i < n; ++i)
r.appendChild(document.createElement("li")).appendChild(document.createTextNode(a[i]));
return r;
};
})({
mitsubishi: ["Eclipse", "Lancer", "Outlander"],
nissan: ["Quest", "Versa", "Altima"]
});
</script>