Array Methods Flashcards
length
length returns how many elements are in the array. This is a property, NOT a function (you can tell because we type length, not length(). As we’ve seen, it can (but is almost never) be used to remove elements/clear an array.
var arr = [1,2,3,4]; arr.length; // 4 arr[arr.length]; // undefined arr[arr.length-1]; // 4 - this is a nice way to access the last element of an array when you don't know how many elements are inside it.
slice
slice makes a copy of an array. We can use it to copy the entire array, or create a copy of a subarray. If we just invoke slice() with no arguments, we’ll create a copy:
var arr = [1,2,3,4]; var copy = arr.slice(); copy; // [1,2,3,4];
Alternatively, you can pass in two arguments to slice. Like splice, the first argument indicates the starting index of the subarray you want. The second argument indicates the ending index. The subarray you get will consist of all the values starting from the starting index and going up to (but not including) the ending index:
var arr = [7, 6, 5, 4, 3, 2];
arr. slice(1, 2); // [6]
arr. slice(2, 5); // [5, 4, 3]
arr. slice(2, 1); // []
concat
concat joins two arrays together.
var arr1 = [1,2,3]; var arr2 = [4,5,6]; var combined = arr1.concat(arr2); combined; // [1,2,3,4,5,6]
In fact, you can pass multiple arrays into concat and it will still return a single array to you:
var arr1 = ["a","b","c"]; var arr2 = ["d","e","f"]; var arr3 = ["g","h","i"]; var combined = arr1.concat(arr2,arr3); combined; // ["a","b","c","d","e","f","g","h","i"];
What’s more, you don’t even need to pass an array into concat! Any comma-separated list of values can be concatenated with the original array:
var openingWords = ["It","was","a"]; var moreOpeningWords = openingWords.concat("dark","and","stormy","night"); moreOpeningWords; // ["It", "was", "a", "dark", "and", "stormy", "night"]
indexOf
indexOf finds the first index of the element passed in (starting from the left). If the element is not found, it returns -1. Here are some examples:
var arr = [1,2,3,4,5,4,4];
arr. indexOf(2); // 1
arr. indexOf(3); // 2
arr. indexOf(1); // 0 - remember, arrays are zero indexed
arr. indexOf(4); // 3 - indexOf stops once it finds the first 4.
arr. indexOf(10); // -1
You’ll see this function very commonly used to check if an element is in an array or not. Here’s an example:
var moviesIKnow = [ "Wayne's World", "The Matrix", "Anchorman", "Bridesmaids" ];
var yourFavoriteMovie = prompt(“What’s your favorite movie?”);
if (moviesIKnow.indexOf(yourFavoriteMovie) > -1) {
alert(“Oh, cool, I’ve heard of “ + yourFavoriteMovie + “!”);
} else {
alert(“I haven’t heard of “ + yourFavoriteMovie + “. I’ll check it out.”);
}
lastIndexOf
lastIndexOf works just like indexOf, but starts searching from the end of the array rather than the beginning.
var arr = [1,2,3,4,5,4,4];
arr. indexOf(4); // 3
arr. lastIndexOf(4); // 6 - this one is different now as it starts from the end!
arr. lastIndexOf(10); // -1 - still returns -1 if the value is not found in the array