ES6 - Multidimensional Arrays



An array element can reference another array for its value. Such arrays are called as multi-dimensional arrays. ES6 supports the concept of multi-dimensional arrays. The simplest form of a multi-dimensional array is a two-dimensional array.

Declaring a Two-dimensional Array

var arr_name = [ [val1,val2,val3],[v1,v2,v3] ] 

Accessing a Two-dimensional Array Element

var arr_name[initial_array_index][referenced_array_index]

The following example better explains this concept.

Example

var multi = [[1,2,3],[23,24,25]]  
console.log(multi[0][0]) 
console.log(multi[0][1]) 
console.log(multi[0][2]) 
console.log(multi[1][0]) 
console.log(multi[1][1]) 
console.log(multi[1][2])  

The above example initially declares an array with 2 elements. Each of these elements refer to another array having 3 elements. Following is the pictorial representation of the above array.

Multidimensional Arrays

While referring to an array element here, the subscript of the initial array element must be followed by the subscript of the referenced array element. This is illustrated in the above code.

The following output is displayed on successful execution of the above code.

Output

1 
2 
3 
23 
24 
25
Advertisements