Skip to content

Commit 413cb31

Browse files
docs: 12-arrays updated
Added explanation and examples for two-dimensional arrays and array methods.
1 parent 3ba6e7f commit 413cb31

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

docs/12-arrays.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,64 @@ A multi-dimensional array stores a combination of values of a single type in two
105105
Figure 11.38 shows the representation of a multi-dimensional array.
106106
![Access Array Elements](images/Figure-11.38-EXCEL.png)
107107

108+
A two-dimensional array is an array of arrays. This means, for a two-dimensional array, first a main array is declared and then, an array is created for each element of the main array.
109+
The syntax to declare a two-dimensional array is follows:
110+
**Syntax**
111+
```
112+
var variable_name = new Array(size); // Declaration
113+
variable_name[index] = new Array('value1', 'value2'..);
114+
```
115+
where,
116+
`variable_name`: Is the name of the array.
117+
`index`: Is the index position.
118+
`value1`: Is the value at the first column.
119+
`value2`: Is the value at the second column.
120+
121+
## Accessing Two-Dimensional Arrays
122+
Multi-dimensional arrays can be accessed by using the index of main array variable along with index of sub-array.
123+
124+
**Access Array Elements Without Loops**
125+
Code Snippet 39:
126+
```
127+
<script>
128+
var employees = new Array(3);
129+
employees[0] = new Array('John', '25', 'New Jersey');
130+
employees[1] = new Array('David', '21', 'California');
131+
document.write('<h3>Employee Details</h3>');
132+
document.write('<p><b>Name: </b>' + employees[0][0] + '</p>');
133+
document.write('<p><b>Location: </b>' + employees[0][2] + '</p>');
134+
document.write('<p><b>Name: </b>' + employees[1][0] + '</p>');
135+
document.write('<p><b>Location: </b>' + employees[1][2] + '</p>');
136+
</script>
137+
```
138+
In the code, `var employee = new Array(3)` creates an array of size 3. The declaration `employee[0] = new Array('John', '23', 'New Jersey')` creates an array at the 0th row of the `employees.array` Similarly, `employees[1] = new Array('David', '21', 'California')` creates an array at the first row of the `employee` array.
139+
140+
Access Array Elements With Loops
141+
```
142+
<script>
143+
var products = new Array(2);
144+
products[0] = new Array('Monitor', '236.73');
145+
products[1] = new Array('Keyboard', '45.50');
146+
document.write(`
147+
<table border=1>
148+
<tr>
149+
<th>Name</th>
150+
<th>Price</th>
151+
</tr>
152+
153+
for(var i = 0; i < products.length i++) {
154+
document.write('<tr>');
155+
156+
for(var j = 0; j < products.[i].length; j++) {
157+
document.write('<td>' + products[i][j] + '</td>')
158+
}
159+
160+
document.write('</tr>');
161+
}
162+
</table>
163+
`);
164+
```
165+
In the code, `products[0] = new Array('Monitor', '236.75')` creates an array at the 0th row of the `products` array. Similarly, `products[1] = new Array('Keyboard', '45.50')` creates an array at the first row of the `products` array. The condition, `i < products.length`, specifies that the counter variable `i` should be less than the number of rows in the array variable, `products`. For each row in the array, the condition, `j < products[i].length` specifies that the counter variable `j`, should be less than the number of columns specified the `ith` row of the array variable, `products.` Finally, `document.write("<td>" + products[i][j] + "</td>")` displays the values at the `ith` row and `jth` column of array variable, products.
108166
## 11.28 Array Methods
109167
An array is a set of values grouped together and identified by a single name. In JavaScript, the `Array` object allows you to create arrays. It provides the `length` property that allows you to determine the number of elements in the array. Various methods of the `Array` object allow to access and manipulate the array elements.
110168

@@ -115,3 +173,26 @@ An array is a set of values grouped together and identified by a single name. In
115173
| `pop` | Retrieves the last element of an array. |
116174
| `push` | Appends one or more elements to the end of an array. |
117175
| `sort` | Sorts the array elements in an alphabetical order. |
176+
## 11.29 *for...in Loop*
177+
The `for...in` loop is an extension of the `for` loop. It enables to perform specific actions on the arrays of objects. The loop reads each element in the specified array and executes a block of code only once for each element in the array.
178+
179+
**Syntax**
180+
```
181+
for(variable_name in array_name) {
182+
// statements;
183+
}
184+
```
185+
where,
186+
`variable_name`: Is the name of the variable.
187+
`array_name`: Is the array name.
188+
189+
```
190+
<script>
191+
var books = new Array('Beginning CSS 3.0', 'Introduction to HTML5', 'HTML5 in Mobile Developent');
192+
document.write('<h3>List of Books</h3>');
193+
194+
for(var i in books) {
195+
document.write(books[i] + '<br/>');
196+
}
197+
</script>
198+
```

0 commit comments

Comments
 (0)