Javascript - Data Types and Variables - Javascript Multidimensional Array
Şubat 12, 2011 by Javascript
|
|
Article Information
This Javascript tutorial contains and tries to cover following subjects:
- Brief explanation of Multidimensional array in Javascript
- Example to Multidimensional array in Javascript
Articles tries to provide answer to following questions and issues:
- How to create a multidimensional array in Javascript
- How to store data, and access elements in multidimensional array in Javascript
Articles pre-requisites following information:
- General knowledge of variables in Javascript
- General knowledge of arrays in Javascript
- General knowledge of functions in Javascript
- General knowledge of HTML
Brief explanation of Javascript Array
In this javascript tutorial, we will try to cover multidimensional array subject to understand how to create a multidimensional array, store data in them and access to array values with a HTML example.
Arrays, similar in other programming languages, can store values - data. They are some sort of data packages if we imagine. In earlier javascript tutorial javascript variables, we had explained javascript variables in detail.
Javascript arrays different from a normal javascript variable, can hold more than one data. For example, we can store some numbers, some text and similar.
If we recall earlier tutorial about javascript arrays, we had created below array.
var v_customer = new Array();
If we decide to store customer names and order count with name in that javascript array, in raw logic, we can add first customer name and then order count. Finally we will have a "4" size javascript array. Lets see in example:
var v_customerData = new Array();
v_customer [0] = "IBM"
v_customer [1] = "Microsoft"
To add data order to customer, we will modify array as follows:
var v_customerData = new Array();
v_customerData [0] = "IBM"
v_customerData [1] = "11098"
v_customerData [2] = "Microsoft"
v_customerData [3] = "68"
In above javascript array variable assignment, we stored names and their order. This is a method. Javascript multidimensional arrays brings better solution to store that kind of designed data. Idea behind a multidimensional array can be think of a table. One dimension is column and another dimension is for rows. Following illustration reflects a concept of multidimensional array:
Referencing above explanation, lets create our javascript multidimensional array and store data in array according to above logic.
var v_customerData [0] = new Array();
var v_customerData [1] = new Array();
v_customerData [0][0] = "IBM"
v_customerData [0][1] = "11098"
v_customerData [1]]0] = "Microsoft"
v_customerData [1][1] = "68"
With that way, we build a javascript two dimensional array. Every dimension above is initialized if you noticed. Ideally, we can think of it like creating every column before adding rows. We enclose index numbers between the square brackets similar to javascript arrays. Difference mainly here is, we added second square bracket to initialized multidimensional array. It is similar to a matrix..
Example for a Javascript Array
In following javascript multidimensional array example, we will create above javascript array in HTML page in javascript block. We will be first declaring an multidimensional array, initialize, assign some values and finally display them in HTML page.
HTML page shows result at the right pane.
Data Layers
Area: | programming \ languages \ javascript \ \ \ |
Ref: | josefh75 |
Loc: | articles |
Tags: | javascript |
|