Arrays Flashcards
What is an array?
- It is a data structure to store ordered collections
- Array elements are numbered starting with zero
- Arrays have many methods to manage the order of elements
- Can be created by a constructor or literal notation
How do you create an array?
you can create a list of movies as follows.
const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];
Everything in the bracket makes up the array
Can an array contain different types of values like string, number, booleans, objects?
Yes
How do you access the array size?
The size of an array is the number of object in it and can be accessed as follows:
const arrayName = [ ]; // Create an empty array console.log(arrayName.length); // 0
How to access a particular element in an array?
You can access a particular element passing its index in squared braket
const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];
console. log(movies[0]); // “The Wolf of Wall Street”
console. log(movies[1]); // “Zootopia”
console. log(movies[2]); // “Babysitting”
How do you update arrays content?
You add a new item to an array with the “push()” method. The new element to be added is passed as a parameter to the method. It is inserted at the end of the array.
const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];
movies. push(“Ghostbusters”);
console. log(movies[3]); // “Ghostbusters”
How do you remove an element from an arrays?
You can use “pop()” or you can use “splice()”:
“pop()” : let you remove the last element
“splice()” : it need two parameters the first one is the index from which to begin removing, and the second one is the number of elements to remove.
E.g
const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];
movies.splice(0, 1);
Main methods to add or remove items from arrays?
- arr.push(…items) – adds items to the end,
- arr.pop() – extracts an item from the end,
- arr.shift() – extracts an item from the beginning,
- arr.unshift(…items) – adds items to the beginning.
What is the syntax for creating an array with
- Literal Notation
- Array constructor
Literal notation: let newArr = [] Array constructor: let newArr = Array()
What are arrays populated with?
Elements
newArr = [“Zebra” , , ture,21]
What is the element at index number 1 and 4
Undefined, because it is an empty space and nothing because the array stops at index 3
How to get the last element from an array?
let lastElement = nameOfArray [nameOfArray - 1 ]
Arrays iteration’
How do you iterate through an array passing the value and index for each elements?
Taking this example of array
let bestColors = [‘green’, ‘blue’, ‘yellow’, ‘black’]
bestColors.forEach((x,i)=> console.log(x,i,a))
x: will display the elements
i: will display the index
a: will display the whole array
What is the method “.forEach” used for?
.forEach is a built in function that allows to to run a function for every element of the array.
What is the syntax of the .forEach method?
arrayName.forEach(function(item, index, array) {
do something with item
});