Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Associative Object Array

Status
Not open for further replies.

RISTMO

Programmer
Nov 16, 2001
1,259
US
Having trouble getting this to work... help please?

Code:
var majorCode = new Object();
var majorDescription = new Object();

var majorCode["BA"] = new Object();
var majorDescription["BA"] = new Object();

var majorCode["BS"] = new Object();
var majorDescription["BS"] = new Object();

var majorCode["BA"][0] = "BIBS";
var majorDescription["BA"][0] = "Biblical Studies";

var majorCode["BA"][1] = "BIOL";
var majorDescription["BA"][1] = "Biology";

var majorCode["BS"][2] = "BIOL";
var majorDescription["BS"][2] = "Biology";

alert(majorCode["BA"][0] + ": " + majorDescription["BA"][0]);

Thanks,
Rick

RISTMO Designs: Rockwall Web Design
Arab Church: Arabic Christian Resources
Genuine Autos: Kaufman Chevrolet & Pontiac Dealer
Rick Morgan's Official Website
 
use Array, not Object and only use keyword "var" when you are declaring the variable, not when setting a property of it
Code:
var majorCode = new Array();
var majorDescription = new Array();

majorCode["BA"] = new Array();
majorDescription["BA"] = new Array();

majorCode["BS"] = new Array();
majorDescription["BS"] = new Array();

majorCode["BA"][0] = "BIBS";
majorDescription["BA"][0] = "Biblical Studies";

majorCode["BA"][1] = "BIOL";
majorDescription["BA"][1] = "Biology";

majorCode["BS"][2] = "BIOL";
majorDescription["BS"][2] = "Biology";

alert(majorCode["BA"][0] + ": " + majorDescription["BA"][0]);

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Thanks man. I'd heard object was faster/better than array when it's multidimensional, but all I need is something that works :-D.

I got another question, though... I took off the numbers for the 2-nd dimension when I populate the arrays (from a database). That seems to be fine, but when I call the function and pass in a value populateMajors("BM") for example, it gives me an object expected error message. I'm confused why it doesn't read how I expected?

Code:
var majorCode = new Array();
var majorDescription = new Array();

majorCode["BM"][] = "THCMP";
majorDescription["BM"][] = "Theory and Composition";
	
majorCode["BM"][] = "THCMP";
majorDescription["BM"][] = "Theory/Composition";
	
majorCode["BBA"][] = "UNDC";
majorDescription["BBA"][] = "Undecided";
	
majorCode["BA"][] = "UNDC";
majorDescription["BA"][] = "Undecided";
	
majorCode["ABA"][] = "UNDC";
majorDescription["ABA"][] = "Undecided";
	
majorCode["BM"][] = "VOPF";
majorDescription["BM"][] = "Voice Performance";
	
function populateMajors(degreeCode){
	var majorOptions = '<option value="">--Please Select--</option>';
	for(var i=0;i<majorCode[degreeCode].length;i++){
		if(majorCode[degreeCode][i]!=undefined)
			majorOptions += '<option value="' + majorCode[degreeCode][i] + '">' + majorDescription[degreeCode][i] + '</option>';
	}
	document.getElementById("majorHolder").innerHTML = '<select name="major" id="major">' + majorOptions + '</select>';
}

Rick

RISTMO Designs: Rockwall Web Design
Arab Church: Arabic Christian Resources
Genuine Autos: Kaufman Chevrolet & Pontiac Dealer
Rick Morgan's Official Website
 
two reasons: you must pass the index otherwise javascript doesn't know where to place the element, and also because you haven't defined the first level elements as arrays in the first place

you could use push() to avoid using explicit indices:

Code:
var majorCode = new Array();
var majorDescription = new Array();

majorCode["BM"] = new Array();
majorCode["BM"].push("THCMP");
majorCode["BM"].push("THCMP");
majorCode["BM"].push("VOPF");

majorDescription["BM"] = new Array();
majorDescription["BM"].push("Theory and Composition");
majorDescription["BM"].push("Theory/Composition");
majorDescription["BM"].push("Voice Performance");
    
majorCode["BBA"] = new Array();
majorCode["BBA"].push("UNDC");

majorDescription["BBA"] = new Array();
majorDescription["BBA"].push("Undecided");

majorCode["BA"] = new Array();
majorCode["BA"].push("UNDC");

majorDescription["ABA"] = new Array();
majorDescription["BA"].push("Undecided");

majorCode["ABA"] = new Array();
majorCode["ABA"].push("UNDC");

majorDescription["ABA"] = new Array();
majorDescription["ABA"].push("Undecided");

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
We're almost there! But this last error makes no sense to me.... it works great for all the degree options except for "Bachelor of Business Admnst." But the same script generated all the options, and they're all in the same format! I don't get it. Any ideas?

Code:
<html>
<body>
<script type="text/javascript">
var majorCode = new Array();
var majorDescription = new Array();

	majorCode["BA"] = new Array();
	majorDescription["BA"] = new Array();

	majorCode["BBA"] = new Array();
	majorDescription["BBA"] = new Array();

	majorCode["BM"] = new Array();
	majorDescription["BM"] = new Array();

	majorCode["BS"] = new Array();
	majorDescription["BS"] = new Array();

	majorCode["BA"].push("BIBS");
	majorDescription["BA"].push("Biblical Studies");

	majorCode["BA"].push("BIOL");
	majorDescription["BA"].push("Biology");

	majorCode["BS"].push("BIOL");
	majorDescription["BS"].push("Biology");

	majorCode["BM"].push("MUCO");
	majorDescription["BM"].push("Choral Music");

	majorCode["BA"].push("CHST");
	majorDescription["BA"].push("Christian Studies");

	majorCode["BS"].push("CHST");
	majorDescription["BS"].push("Christian Studies");

	majorCode["BA"].push("COSC");
	majorDescription["BA"].push("Computer Science");

	majorCode["BS"].push("COSC");
	majorDescription["BS"].push("Computer Science");

	majorCode["BA"].push("ENGL");
	majorDescription["BA"].push("English");

	majorCode["BS"].push("ENGL");
	majorDescription["BS"].push("English");

	majorCode["BA"].push("GENS");
	majorDescription["BA"].push("General Studies");

	majorCode["BS"].push("GENS");
	majorDescription["BS"].push("General Studies");

	majorCode["BA"].push("HIST");
	majorDescription["BA"].push("History");

	majorCode["BS"].push("HIST");
	majorDescription["BS"].push("History");

	majorCode["BA"].push("INTA");
	majorDescription["BA"].push("Interdisciplinary Academic");

	majorCode["BS"].push("INTA");
	majorDescription["BS"].push("Interdisciplinary Academic");

	majorCode["BA"].push("KNES");
	majorDescription["BA"].push("Kinesiology");

	majorCode["BS"].push("KNES");
	majorDescription["BS"].push("Kinesiology");

	majorCode["BA"].push("MATH");
	majorDescription["BA"].push("Mathematics");

	majorCode["BS"].push("MATH");
	majorDescription["BS"].push("Mathematics");

	majorCode["BA"].push("MUSI");
	majorDescription["BA"].push("Music");

	majorCode["BS"].push("MUSI");
	majorDescription["BS"].push("Music");

	majorCode["BA"].push("MUSB");
	majorDescription["BA"].push("Music Business");

	majorCode["BS"].push("MUSB");
	majorDescription["BS"].push("Music Business");

	majorCode["BA"].push("NASC");
	majorDescription["BA"].push("Natural Sciences");

	majorCode["BS"].push("NASC");
	majorDescription["BS"].push("Natural Sciences");

	majorCode["BM"].push("ORPF");
	majorDescription["BM"].push("Organ Performance");

	majorCode["BA"].push("PHIL");
	majorDescription["BA"].push("Philosophy");

	majorCode["BS"].push("PHIL");
	majorDescription["BS"].push("Philosophy");

	majorCode["BM"].push("PIPF");
	majorDescription["BM"].push("Piano Performane");

	majorCode["BA"].push("POLS");
	majorDescription["BA"].push("Political Science");

	majorCode["BS"].push("POLS");
	majorDescription["BS"].push("Political Science");

	majorCode["AA"].push("TCP");
	majorDescription["AA"].push("Teacher Certification Program");

	majorCode["BM"].push("THCMP");
	majorDescription["BM"].push("Theory and Composition");

	majorCode["BM"].push("THCMP");
	majorDescription["BM"].push("Theory/Composition");

	majorCode["BBA"].push("UNDC");
	majorDescription["BBA"].push("Undecided");

	majorCode["BA"].push("UNDC");
	majorDescription["BA"].push("Undecided");

	majorCode["ABA"].push("UNDC");
	majorDescription["ABA"].push("Undecided");

	majorCode["BM"].push("VOPF");
	majorDescription["BM"].push("Voice Performance");

function populateMajors(degreeCode){
	var majorOptions = '<option value="">--Please Select--</option>';
	for(var i=0;i<majorCode[degreeCode].length;i++){
		if(majorCode[degreeCode][i]!=undefined)
			majorOptions += '<option value="' + majorCode[degreeCode][i] + '">' + majorDescription[degreeCode][i] + '</option>';
	}
	document.getElementById("majorHolder").innerHTML = '<select name="major" id="major">' + majorOptions + '</select>';
}

</script>

Degree<br />
<select name="degree" onChange="populateMajors(this.options[this.selectedIndex].value);">
	<option value="">--Please Select--</option>
	<option value="BA">Bachelor of Arts</option>
	<option value="BBA">Bachelor of Business Admnst.</option>
	<option value="BM">Bachelor of Music</option>
	<option value="BS">Bachelor of Science</option>
</select>
<br />
Major<br />
<div id="majorHolder">
<select name="major" id="major">
<option value="">--Please select a degree--</option>
</select>
</div>
</body>
</html>

Thanks!
Rick

RISTMO Designs: Rockwall Web Design
Arab Church: Arabic Christian Resources
Genuine Autos: Kaufman Chevrolet & Pontiac Dealer
Rick Morgan's Official Website
 
looks like you forgot


majorCode["AA"] = new Array();
majorDescription["AA"] = new Array();

majorCode["ABA"] = new Array();
majorDescription["ABA"] = new Array();




-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top