Arrays
As well as the basic data types that we have covered, data can also be represented in a couple of more complex forms. The simplest of these is an array.
An array is just a list of values. The look like this:
The values in an array can be any valid JavaScript values - strings, booleans, numbers, objects etc. You can even have an array of arrays.
The items in an array do not have to be of the same type
However, you would typically want the items to be of the same type.
An item in an array is typically referred to as an element.
The position of an item in an array is referred to as it's index.
Arrays are zero-indexed, meaning that you start counting from zero (i.e. The first item in an array has index 0).
You can refer to an element by its index by putting the index in square brackets after the array:
You can find how many elements an array has using the length
property:
Arrays come with a lot of handy built in functions to do things like get individual items, or to add or remove items from the list.
One of the most useful features of arrays are iterator or enumerator functions.
These functions take another function as a parameter (this technique is called a callback, and is very common in JavaScript). The callback function is then applied to every element in the array, in order to perform some transformation on the array.
The most common enumerators are:
map
- transforms every item in the array.filter
- removes some items from the array, based on some criteria.reduce
- combines all of the array values into a single value.
So say you had a function that added 1
to a number:
and an array of numbers:
You could use map
to apply the addOne
function to every element in the array:
Or if you weren't planning on using the addOne
function anywhere else, you could omit its declaration:
Both of these would return the same array: [2, 3, 4]
.
Strings also have a function to split the characters into an array:
One important thing to be aware of with array functions is whether they mutate the original array (modify it), or return a new one:
It is important to be aware of this because if the original array is altered unexpectedly, then you can easily introduce bugs into your code.
There are 15 challenges to complete involving arrays, many of which involve using enumerators.
To run the tests for this challenge, run npm test -- arrays
In the same manner as before, try to fix all of the failing tests.
- Run the tests
- Read the error message
- Write some code to fix that message
- Repeat
And again, commit to git regularly, and use the links provided and google to help you if you get stuck!
Then move on to the next challenge, linked below.