Arrays in JavaScript Flashcards
What are arrays?
A data type that deals with long lists of information.
How do you create an array?
Use two square brackets and separate the items in it with commas like so:
let myArray = [1, ‘two’];
How do you access an item in an array?
Ask for the item in a given position in the array. For example:
let myArray = [1, 2, 3];
myArray[0]; // => answer is 1
What is the first number in an array list?
0
How do you add an item to the end of an array list?
Use the built-in .push method like so:
let myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray); // => [1, 2, 3, 4]
Can use you const for an array?
Yes, even though the values in it may change. You can use both let and const.
How do you determine how long an array is?
Use the .length method like so:
const myArray = [1, 2, 3, 4]; console.log(myArray.length); // => 4
What is the .pop() array method?
.pop() is used to “pop” a value off of the end of an array. We can store this “popped off” value by assigning it to a variable. In other words, .pop() removes the last element from an array and returns that element. Any type of entry can be “popped” off of an array - numbers, strings, even nested arrays.
var threeArr = [1, 4, 6]; var oneDown = threeArr.pop();
console. log(oneDown); // Returns 6
console. log(threeArr); // Returns [1, 4]
What is the .shift() array method?
Like .pop(), but removes the first item of a list:
const myArray = [1, 2, ‘three’];
console.log(myArray.length); // Returns 3
const firstItem = myArray.shift();
console. log(firstItem); // Returns 1
console. log(myArray.length); // Returns 2
What is this array method? .slice(begin, end)
This method creates a new array from an existing one. If you call it with no arguments, the entirety of the original array will be copied. It can be called with begin and end to specify only part of the original array has been copied. Finally, you can use negative values for the begin parameter in order to select the last n numbers of an array.
const myArray = [1, 2, 3, 4];
myArray.slice(); // => [1, 2, 3, 4]
const copyArray = myArray.slice(0, 2) console.log(copyArray); // => [1, 2]
myArray.slice(-2); // => [3, 4];
What is this array method? .sort()
This method sorts an array in place. In place means that this method sorts the original array; it does not copy the array, sort it, and then return the sorted copy. If you pass no arguments to .sort(), it will alphabetically sort the list by default:
const myArray = ['zebra', 'yodel', 'xylophone']; myArray.sort(); console.log(myArray); // => ['xylophone', 'yodel', 'zebra']
How do you sort an array by criteria?
If you need to sort an array by some other criteria, you must pass in a compare function to .sort() that tells the computer the criteria for sorting any 2 items. This function must return a numeric value. If the returned value is negative, the second element comes before the first. If it’s postive, the first element comes before the second. And if it’s exactly 0, the 1 elements have the same numeric value.
The following example demonstrates how you can sort a list of numbers in ascending order.
const myArray = [200, 20, 2, 100, 1, 10];
console.log(myArray); // => [200, 20, 2, 100, 1, 10]
function sortNumbers(a, b) { return a - b; }
myArray.sort(sortNumbers);
console.log(myArray) // => [1, 2, 10, 20, 100, 200]
What is this array method? .map()
This method is used to generate a new array of items by applying the same function to each item in the original array. Here we provide an example of using map to work with numbers, first using a named function, then with an anonymous (arrow) function:
function double(num) { return 2 * num; }
const myNumbers = [1, 2, 3, 4]; const doubledNumbers = myNumbers.map(double); console.log(doubledNumbers); // => [2, 4, 6, 8];
const doubledNumbersAlt = myNumbers.map(num => 2 * num); console.log(doubledNumbersAlt); // => [2, 4, 6, 8];
What is this array method? .forEach()
Like .map(), this method applies a function to each item in a collection, but it does not return an array of transformed elements.
const directionsLibrary = [‘wash’, ‘rinse’, ‘repeat’];
function saveDirectionInDatabase(direction) { // save the direction in the database console.log(direction); }
directionsLibrary.forEach(saveDirectionInDatabase); // -> // 'wash' // 'rinse' //'repeat'
// or directionsLibrary.forEach(direction => console.log(direction)); // -> // 'wash' // 'rinse' //'repeat'
What is this array method? .filter()
This method is used to take one array of items and make a new one that contains only items that a filtering function returns true for.
function isEven(num) { return num % 2 === 0; }
const myNumbers = [1, 2, 3, 4, 5, 6];
const evens = myNumbers.filter(isEven); console.log(evens); // => [2, 4, 6]
// or const evensAlt = myNumbers.filter(num => num % 2 === 0); console.log(evensAlt); // => [2, 4, 6];