Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Javascript
Dis-Array??
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Anthony Borla, post: 4886488"] By the looks of things it's not a 2D array that you are after, but rather, a 1D array of aggregate objects [yes, the distinctions *are* rather blurry :)]. The following code [it's actually JScript executed under WSH but only minor adjustments are needed for browser-based execution] should help you understand the difference. Note that multiple ways of performing a particular task are shown, marked (1) and (2), and one of them is always commented out. // --------------------- function Address(fname, lname, city) { this.fname = fname; this.lname = lname; this.city = city; // *** Following line not strictly necessary // as it defaults to this action return this; } function showMy1DArray() { /* === // (1) var my1DArray = new Array({fname:"f1", lname:"l1", city:"c1"}, {fname:"f2", lname:"l2", city:"c2"}); === */ // or (2) var my1DArray = new Array(new Address("f1", "l1", "c1"), new Address("f2", "l2", "c2")); /* === // (1) for (var i in my1DArray) { WScript.Echo(my1DArray[i].fname + " " + my1DArray[i].lname + " : " + my1DArray[i].city); } === */ // or (2) for (var i in my1DArray) { for (var j in my1DArray[i]) { WScript.Echo("[" + i + "][" + j + "] = " + my1DArray[i][j]); } } } function showMy2DArray() { /* === // (1) var my2DArray = new Array(new Array(), new Array()); my2DArray[0][0] = "a"; my2DArray[0][1] = "b"; my2DArray[0][2] = "c"; my2DArray[1][0] = "A"; my2DArray[1][1] = "B"; my2DArray[1][2] = "C"; === */ // or (2) var my2DArray = new Array(new Array("a", "b", "c"), new Array("A", "B", "C")); for (var i = 0; i < my2DArray.length; i += 1) for (var j = 0; j < my2DArray[i].length; j += 1) WScript.Echo("[" + i + "][" + j + "] = " + my2DArray[i][j]); } showMy2DArray(); showMy1DArray(); // --------------------- More information may be obtained from the FAQ: [URL]http://www.jibbering.com/faq/faq_notes/square_brackets.html[/URL] or from Douglas Crockford's site: [URL]http://www.crockford.com/[/URL] Be patient, and keep at it - understanding will come with time. I hope this helps. Anthony Borla[/i][/i][/i][/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Javascript
Dis-Array??
Top