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 / 7
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

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

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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