Properties and Methods of Custom 2D JavaScript Arrays - Adding Cells
(Page 4 of 4 )
After adding a row, the row is empty. You do not really need any function to add a cell. Rows are object initializers. By just assigning a value to any cell, you create (add) a cell with its value.
Let us look at an example. Consider the CustomArray() array object type that we have created. Assume that you have created the myArray object from CustomArray(). Assume that the row you inserted is at index 2. Below the constructor function in the code, you can type,
myArray[2][3] = “val23”;
The row you have just inserted is blank and is at row index 2. This statement creates a cell at the position of cell index 3 in the row, and gives it the value “val23.”
Note that you cannot add a cell like this to a row that does not exist or has not been inserted. If you want to add a cell to a row that is not in the array, first insert the row as shown above. Remember that the method of insertion shown above can be used to insert a row outside the range of rows that are already there.
Assigning Values
You can assign a value to any cell in any row that already exists in the array. If you want to assign a value to a cell in a row that does not exist, you have to insert that row first, and then you assign a value to any cell in the inserted row. When you assign a value to any cell in the inserted row, the cell is automatically created. If the cell was already there, the value (content) is replaced. A statement like the following will either create a new cell and its value in any row that exists, or have the value of the identified cell replaced:
myArray[4][1] = “val23”;
Having Other Properties and Methods
You can give a property to the array object type, as we gave the height property. You can give a method as we gave the insertRow method. If you want, for example, a method to sort the data in a column, you need to know how to write a sort function first; then you type the function into the constructor function as we typed the insertRowFn(i) function above. You make this function a method by declaring a method, as we did for the insertRow method.
Always remember that your 2D array object type is an object of object constructor function. So you give properties and methods as you would to any object.
Follow the link below for the complete code.
Custom 2D JavaScript Array.ZIP
Thanks for reading this two-part series.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |