Because arrays are awesome! You need them! And we need to focus a lot on them in order to prepare you for real life 🙂
Mastering the use of arrays and loops is one of the 5 fundamental skills of building algoritms:
No no no…Wait: Arrays? What?
An array is, normally, any list or collection of values. The rules of how to add or remove elements from that list can change from one programming language to another. But – generally – they are the only ways for developers to create elements.
Arrays are the only way we have to list stuff – regardless of the application you are working with, it will always have things to list. For example: list of students, list of artists, list of transactions…anything!
This data-type does a lot more stuff than the others. Lists are the only way to store more than one data-type in the same variable.
Every array has the same basic concepts:
The items: are the actual values inside in each position of the array.
The length: is the size of the array (how many items the array has).
Index: is the position of the element.
☝Array positions start at zero (0); the first element is the element in the position zero (0)
These are different examples of list declarations:
☝ Don't declare the Arrays with the
new Array()syntax – it will not behave properly click here to learn the details
To access a specific element in a list, you need an
index. We call
index the integer value that represents the position of the element you want to access/get/retrieve.
The index always must start at zero (0). That means that an Array of 2 items can have index=0 or index=1. Trying to get the 2nd position will return "undefined" because it will mean that we are trying to access the third element (which does not exist). For example, to get any items in the array you can do the following:
If you want, you can reset or update any item inside of an array using the index like this:
The only way to add a new element is at the end of the list, and you will need to use the push() function for that.
But… what if I want to add Chris in the second position?
Then… you need to create a new empty array and start pushing the elements in the order that you need them. In this case it will be:
Removing an element has the exact same limitations as in adding an element: you can only remove an element from the last position using the pop() method. If you want to remove a different element, you will need to create a new array without that particular element.
The shift and unshift methods are just like push and pop but with the difference that they will only work from the very beginning of the list.
Sometimes, when working with arrays, you will need to loop them. For example: sorting them manually; flipping them; deleting an element from a particular position, etc.
In order to create your loop you will need to use Array.length to get the current size of the array. Most of the time the array items change during the runtime. This is why the only way to get the size of the array will be using the array.length function, like this:
There is a great adaptation of the for statement to make it loop lists or arrays, like this:
Variables can have different types of values. Some of them are available only in specific programming languages, but, almost all of them have the following types:
It is possible to cut an array in pieces very fast, with the splice and slice functions.
Will return a new array with a smaller version of the original array. You have to specify the starting and ending index from where you want to cut the array.
Will update the current array leaving everything but the smaller version that you want to remove. You will need to specify the starting and ending indexes from where you want to remove.
☝ Splice can accept as many optional paramenters as wanted and those will substitute the part of the array that has been deleted. The first parameter is the index where the deletion starts, the second how many elements will be deleted and from the third onward the elements inserted after the position set by the first parameter.
We can use this function to insert elements:
Sort and Reverse
They do the same, except reverse does it backwards. They both sort using string comparison logic – which means that the string "25" is bigger than "100", because "2" is bigger than "1".
If you want to sort real numbers, or if you want to use any other type of login to sort arrays, you have to use a "comparison function."
You have to define a function that will take care of the comparisons. The sort function will call your function on each comparison and will let your function decide who comes first between both of the elements that are being compared.
Now that we know about the comparison function, we can use it to tell the sort function how to sort our own special objects – like here for example:
All rights reserved, 4Geeks LLC 2021. Read more about 4Geeks and what we are going here.