You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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');
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.
108
166
## 11.28 Array Methods
109
167
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.
110
168
@@ -115,3 +173,26 @@ An array is a set of values grouped together and identified by a single name. In
115
173
|`pop`| Retrieves the last element of an array. |
116
174
|`push`| Appends one or more elements to the end of an array. |
117
175
|`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');
0 commit comments