What are arrays ?

array header

Arrays are an ordered list of values.

 

Let’s Play a Game! Can You Guess What Moved? – Nothing

Let’s say you and a friend some have toys you want to place in a container. You decide to make a game out of it. You place your toys in a a box. Your friend tells you to close your eyes and guess what changed. When you open your eyes again everything is in the same order nothing changed. This is what an ordered list of values means. Arrays keep the same order of the items you put inside of them.

Let’s Play a Game! Can You Guess What Moved? – Something

Now imagine this scenario. You place your toys in a a box and line them up right to each other. Your friend tells you to close your eyes and this time they change around the order of your toys. All your toys are still there but they aren’t in the same order. This would be considered a map. Map also goes by the name of dictionary or hash table. All of these words mean the same thing.

Maps aren’t an ordered list, they are an unordered. You can guarantee the order will be the same as what you set it to be. Maps order things as they see best.

Multi-dimensional Arrays

This is a fancy way of saying I’m a large box that has smaller boxes inside of me. Just like arrays multi-dimensional arrays are ordered.

How Can You Confirm the Order?

Arrays

Let’s say you want to look through an array. Looking over arrays are simple since the array is ordered. All you have to know is you start counting from 0 and continue for as many items as you have.

If you have an array with the following values:

let arr = ["doll", "car", "iPhone", "minion"];

You can view what’s inside of the array by using a for-loop:

for(var i = 0; i < arr.length; i++) {
console.log(i + " " + arr[i]);
}
 // This will print out the following
// 0 "doll"
// 1 "car"
// 2 "iPhone"
// 3 "minion"

Maps

Iterating over a map is simple as well but since the map isn’t ordered you go through it in a different way. You can uses number to find an item in your map. They use a key and value pair. Key is a special name for your actual value.

If you have an map with the following values:

let toyBox = [{"toy1": "doll"}, {"toy2": "car"}, {"toy3": "iPhone"}, {"toy4": "minion"}]

for(const [key,value] of Object.entries(toyBox)) {
console.log(key +  " " + value);
}
 // This will print out the following
// "toy1" "doll"
// "toy2" "car"
// "toy3" "iPhone"
// "toy4" "minion"

If you like this article or would like a specific topic explained drop a comment below. I would love to hear your feedback!

Leave a comment

Your email address will not be published. Required fields are marked *