Data Structures: Arrays, Sets, and Maps.
Data structures are used to store lists of values, these data structures are called arrays, sets, and maps. But what is data structure? According Wikipedia, “In computer science, a data structure is a particular way of organizing data in a computer, so it can be used efficiently”, to which I would add that using logical statements allows you to control the flow of a program, as well as loops that would allow us to repeat blocks of code over and over again as needed in our code.
The Array Data Structure
An array data structure or an array is an ordered list of values, or a collection of elements (values or variables) identified by an index or key. The most simple type of array data structure is a linear array.
Creating an array literal will require you to write a pair of square brackets.
const myArray = [ ]; << [ ]
We can add a constructor function
const myArray = new Array( ); << [ ]
Arrays are not primitive values but a special built-in object, like when using the “typeof” operator:
typeof [ ]; << ‘object’
Initializing an array
But, How do we initialize an array? Well, we can create an empty array literal.
const heroes = [ ];
Now, we can find the value of element 0 (zero) in our heroes array
heroes ; // to access a specific value in an array, we write its index inside the square brackets << undefined
Adding values to an array
Place a new string by assigning the element 0 (zero).
heroes = ‘Superman’;
We can treat items in the array as a variable and changing its value by using the assignment operator =
heroes = ‘Batman’;
We can add more values using different indices:
heroes = ‘Wonder Woman’; heroes = ‘Flash’; heroes = ‘Aquaman’;
You can repeat the operation until you add all the elements you want or need in your array.
Then, you can review your array by typing your array name (heroes) on your console and look at it.
heroes; << [‘Batman’, ‘Wonder Woman’, ‘Flash’, undefined, undefined, ‘Aquaman’]
You can see that those elements assigned with an index appear in the list by its name, but any other unused slots appear as undefined.
Creating array literals
We can create array literals to avoid adding values one by one.
const avengers = [‘Captain America’, ‘Iron Man’, ‘Thor’, ‘Hulk’]; << [‘Captain America’, ‘Iron Man’, ‘Thor’, ‘Hulk’]
Removing values from arrays
Using the ‘delete’ operator allows us to delete an item from an array.
delete avengers; << true
The action will delete the value store in the index 3 (‘Hulk’), but it also leaves the index as undefined because even when deleting a value the space is still there, meaning that the array still has the same number of elements but the one we deleted will be undefined.
avengers; << [‘Captain America’, ‘Iron Man’, ‘Thor’, ‘undefined’]
Destructuring refers to take values out of the arrays and presenting as individual values, this allows us to assign multiple values at the same time.
const [x, y] = [1, 2];
It means that each variable exists outside the array and you can check its value individually.
x << 1 y << 2
[x, y] = [y, x]; x << 2 y << 1