Multidimensional Arrays:
For this page, all functions()
use the following [arrays]
and functions()
, which have been declared globally:
let eng = ["England", ["London"]];
let wal = ["Wales", ["Cardiff"]];
let sco = ["Scotland", ["Edinburgh"]];
let nir = ["Northern Ireland", ["Belfast"]];
let ire = ["Eire", ["Dublin"]];
let britain = new Array(eng, wal, sco);
let ireland = new Array(nir,ire);
let grbr1 = new Array (eng, wal, sco, nir);
let grbr2 = new Array (britain, nir);
let britisles1 = new Array(eng, wal, sco, nir, ire);
let britisles2 = new Array(grbr1, ire);
let britisles3 = new Array(grbr2, ire);
let britisles4 = new Array(britain, nir, ire);
let britisles5 = new Array(britain, ireland);
Above, we have a series of [arrays]
that detail the make up of the British Isles.
The British Isles are made up of 5 different nations:
- England
- Wales
- Scotland
- Northern Ireland
- Eire
(capital, London)
(capital, Cardiff)
(capital, Edinburgh)
(capital, Belfast)
(capital, Dublin)
and two land masses:
- Britain
- England
- Wales
- Scotland
- Ireland
- Northern Ireland
- Eire
consisting of:
consisting of:
Each individual conutry is represented in its own multidimensional [array]
, containing the country and its capital city.
Thus eng
is an [array]
with 2 dimensions:
eng
= [First-level [array]
is the country, "England", [2nd level is its capital, "London"]]
Further, as we make up the island of Britain, we now have an [array]
of
3 first-level [arrays]
, (that is, England, Wales and Scotland), each of these having a sub-array for their capitals.
Thus britain
is an [array]
with 3 entries, but two dimensions for each entry:
britain
= [[First-level [array]
, "England", [2nd level, "London"]], [First-level array, "Wales", [2nd level, "Cardiff"]],
[First-level [array]
, "Scotland", [2nd level, "Edinburgh"]]];
Further, to make up the "union" of Great Britain, we can do this in a variety of ways:
- (List each country and (capital))
- ((List Northern Ireland and its (capital)), plus (each country and (capital) of Britain)))
((England, (London)), (Wales, (Cardiff)), (Scotland, (Edinburgh)), (Northern Ireland, (Belfast)))
Result: 4 x two-level [arrays]
((Northern Ireland, (Belfast)), ((England, (London)), (Wales, (Cardiff)), (Scotland, (Edinburgh))))
Result: 2 x two-level [arrays]
(Northern Ireland, Britain), with the first of these (Northern Ireland) holding a 2nd-level [array]
(Belfast) and the second two-level [array]
with 2 levels for each item (1st: country, 2nd: capital)
To select and use elements in the [arrays]
, we need to know their index
(remember, JavaScript uses zero indexing!)
For a one-dimensional [array]
, we can just select its "place" within the [array]
:
And so we can for a two-dimensional [array]:
But what happens if that second-level of the [array]
contains more than one element?
Lets first push()
an entry into the [array]
:
We can see that we now have an array that contains the following:
(England, (London), Westminster)
whereas we want
(England, (London, Westminster))
So let's remove Westminster using the pop()
method and then start again:
And now let's add Westminster using the push()
method and with indexing:
So now how do we get to Westminster? We use multidimensional indexing:
Now let's add the following London boroughs into the London level (2nd-level) of our [array]
"eng" (note, we will not use the keys for this exercise):
Map Number | Name | Authority | Control | Population |
---|---|---|---|---|
1 | City of London | Corporation of London | ? | 7,003 |
2 | Westminster | Westminster City Council | Conservative | 226,841 |
3 | Kensington and Chelsea | Kensington and Chelsea London Borough Council | Conservative | 155,594 |
4 | Hammersmith and Fulham | Hammersmith and Fulham London Borough Council | Labour | 178,685 |
But wait, we now have Westminster listed twice, once on its own and once with its borough details.
Use findIndex() and indexOf() to
find Westminster in the [array]
:
As we cannot use pop() or shift() to remove Westminster, we have to use splice() or filter() to remove it:
Using either method above, we have succeeded in removing "Westminster" from the [array]
. However,
from reading the console, we can see that the population of each borough is listed twice (example, for [city], we have an entry "7"
and an entry "3"). Let's now use splice() once again to remove these two enttries and insert one entry
(example, for [city], 7003) in their place:
Clear the console by typing "console.clear()" and returning.
Now type "eng;" then return in the console.
Without clearing the console, now type "britain;" then return.
Without clearing the console, now type "britisles1;" then return.
Without clearing the console, now type "britisles2;" then return.
Without clearing the console, now type "britisles3;" then return.
Without clearing the console, now type "britisles4;" then return.
Without clearing the console, now type "britisles5;" then return.
Use the drop dwon arrows on each returned result to look for the population (that is, index #4) for each of the London Boroughs listed.