offline version v3


⊗jsPmMuAr 168 of 502 menu

Multidimensional arrays in JavaScript

Array elements can be not only strings and numbers, but also arrays. In this case, we get an array of arrays, or a multidimensional array.

In the following example, the array arr consists of three elements, which in turn are arrays:

let arr = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];

Let's rewrite in a more understandable way:

let arr = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ];

Depending on the level of nesting, arrays can be two-dimensional - an array of arrays, three-dimensional - an array of arrays of arrays (and so on - four-dimensional, five-dimensional, etc.).

The above array is two-dimensional, since there are other sub-arrays inside one array, and there are no other arrays already in these sub-arrays.

To display any element from a two-dimensional array, you should write not one pair of square brackets, but two:

let arr = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ]; console.log(arr[0][1]); // shows 'b' console.log(arr[1][2]); // shows 'f'

Given the following array:

let arr = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l'], ];

Use it to output elements with the text 'l', 'e', 'g' and 'a'

Given the following array:

let arr = [[1, 2], [3, 4], [5, 6]];

Referring to each array element, find the sum of all its elements.

enru