Home arrow JavaScript arrow Page 4 - A Custom 2D JavaScript Array
JAVASCRIPT

A Custom 2D JavaScript Array


If you do not like the normal way of creating a two-dimensional array, for any particular reason, then this article is for you. Let me emphasize here that there's already a strong and growing need to make web pages active. So you need to start thinking about writing a custom two-dimensional JavaScript array. You will see how easy it is to do this.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
October 06, 2009
TABLE OF CONTENTS:
  1. · A Custom 2D JavaScript Array
  2. · The Array Properties and Methods
  3. · Object Type Array with its Contents
  4. · Object Type Array Without Contents
  5. · The Constructor Function with a Number of Rows and Columns

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
A Custom 2D JavaScript Array - Object Type Array Without Contents
(Page 4 of 5 )

If you want an object type without contents, set the content of each cell to undefined. The following code demonstrates this:

<html>

<head>

<script type="text/javascript">

function CustomArray()

{

this[0] = {0:undefined,1:undefined,2:undefined,3:undefined,4:undefined,5:undefined,
5:undefined};

this[1] = {0:undefined,1:undefined,2:undefined,3:undefined,4:undefined};

this[2] = {0:undefined,1:undefined,2:undefined,3:undefined,4:undefined,5:undefined};

this[3] = {0:undefined,1:undefined,2:undefined,3:undefined,4:undefined,2:undefined,
5:undefined};

this[4] = {0:undefined,1:undefined,2:undefined,3:undefined};

this[5] = {0:undefined,1:undefined,2:undefined,3:undefined,4:undefined};

}

 

myArray = new CustomArray();

 

</script>

</head>

<body>

<button type="button" onclick="alert(myArray[2][3]);">Click</button>

</body>

</html>

Regular Empty 2D Array

If you want a one-dimensional array with 5 uninitialized elements, you would type:

theArray = new Array(5);

We need a similar thing for our 2D array such that, if we want an array of 4 rows and 3 columns, we would type

myArray = new CustomArray(4,3);

So, to create a 2D array, the syntax would be

arrayObjectName = new CustomArray(y,x);

where y is the number of rows and x is the number of columns.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials