loading words...

Jan 16, 2019 16:15:04

Understanding Multi-Dimensional Arrays in JavaScript

by @jdaniel_richer | 203 words | 🐣 | 61💌

JDaniel Richer

Current day streak: 0🐣
Total posts: 61💌
Total words: 16924 (67 pages 📄)

*This 200wad assumes that you already have at least a basic understanding of arrays and bracket notation in JavaScript*

A multi-dimensional array or nested array is simply an array within an array.

Example (non prettified layout for simple viewing): 

So, to access "Boston" using bracket notation, we must keep zero indexing in mind and count the number of arrays inside the array up to where our element is located, so that would be [0]. Then we need to count the number of elements in the array up to the position of our element, so that would be [3].

Now let's turn up the heat and add a bit more nesting to really challenge ourselves to see if we can still figure out how to access the elements in our deeply nested array.

[1] refers to the array containing "London"

[3] refers to the element in the third position which is the array containing the string, "English".

[0] refers to the first element inside of that array which is "English", therefore that string gets printed to the console.

Try accessing the population of Boston or nesting more arrays and then accessing those values to really challenge yourself. Feel free to add more cities or elements as well. 

From JDaniel Richer's collection:

  • 1

    @jdaniel_richer how did you append the images!? Curious to learn! Though not going to add images to every post haha

    Jason Leow avatar Jason Leow | Jan 17, 2019 13:01:32
    • 1

      @jasonleow Just drag and drop

      JDaniel Richer avatar JDaniel Richer | Jan 17, 2019 04:59:31
    • 1

      @jdaniel_richer oh duh haha never knew! Awesome thank you Daniel

      Jason Leow avatar Jason Leow | Jan 17, 2019 18:35:05
contact: email - twitter / Terms / Privacy