JS - Array Methods
30 flashcards · Programming Languages · by zeckdude
How do you determine how many elements are in an array Provide example using the supplied code: arr = [1, 2, 3] Get the number of elements in the array
arr.length // returns 3
How do you get the element at a specified index? Provide example using the supplied code: arr = [1, 2, 3] Get the value of the element at index 1
arr[1] // returns 2
How do you make a shallow copy of an array? Hint: There are four ways that can be used to solve this. Provide all 4 approaches in the example. Provide example using the supplied code: arr = [1, 2, [3, 4]] Return a shallow copy of the array
arr.slice() // returns [1, 2, [3, 4]] [...arr] // returns [1, 2, [3, 4]] Array.from(arr) // returns [1, 2, [3, 4]] arr.concat() // returns [1, 2, [3, 4]]
How do you create an array of elements for each character in a string? Hint: There are three methods that can be used to solve this. Provide all 3 approaches in the example. Provide example using the supplied code: str = 'sup' Create an array of elements for each character in the string
Array.from(str) // returns ["s", "u", "p"] str.split('') // returns ["s", "u", "p"] Array.prototype.slice.call(str) // returns ["s", "u", "p"]
What is the array method to determine if an array is an array? Provide example using the supplied code: arr = [1, 2, 3] Determine if the array is an array
Array.isArray(arr) // returns true
How do you merge two or more arrays? Hint: There are three ways that can be used to solve this. Provide all 3 approaches in the example. Answer the following question: Does this method mutate the array? Provide example using the supplied code: num1 = [1, 2, 3] num2 = [4, 5, 6] num3 = [7, 8, 9] Merge all 3 arrays together in this order: num1, num2, and num3
Answer: It does not mutate the array. It instead returns a new array. num1.concat(num2, num3) // returns [1, 2, 3, 4, 5, 6, 7, 8, 9] [...num1, ...num2, ...num3] // returns [1, 2, 3, 4, 5, 6, 7, 8, 9]
What is the array method to determine if each element in an array passes a test? Provide example using the supplied code: arr = [1, 2, 3] Test to pass: element < 5 Determine if each element is less than 5
arr.every(function(element) { return element < 5; }); // return true
What is the array method to fill all the elements of an array from a start index to an end index with a specified value? Answer the following question: Does this method mutate the array? Is the end index included? What does it return Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Demonstrate filling all elements, from the 2nd to the 4th position, with the value of 8 Demonstrate filling all elements, from the 3rd position to the end, with the value of 8 Demonstrate filling all el…
Answer: It does mutate the array. The end index is not included. It returns the modified array. arr.fill(8, 1, 4) // returns [1, 8, 8, 8, 5] arr.fill(8, 2) // returns [1, 2, 8, 8, 8] arr.fill(8) // returns [8, 8, 8, 8, 8]
What is the array method to filter out elements that don't pass a test? Answer the following question: Does this method mutate the array? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Test to pass: element < 3 Return the array with only the values that are less than 3
Answer: It does not mutate the array. It instead returns a new array. arr.filter(function(element) { return element < 3; }) // returns [1, 2]
What is the array method to return the first value in an array that passes a test? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Test to pass: element > 3 Return the first value that is greater than 3
arr.find(function(element){ return element > 3; }) // returns 4
What is the array method to return the index of the first value in an array that passes a test? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Test to pass: element > 3 Return the index of the first value that is greater than 3
arr.findIndex(function(element){ return element > 3; }) // returns 3
What is the array method to loop through each element in an array? Provide example using the supplied code: arr = [1, 2, 3] Demonstrate console logging each element
arr.forEach(function(element){ console.log(element); }); // prints: // 1 // 2 // 3
What is the array method to determine if any element in an array passes a test? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Test to pass: element === 3 Determine if any element in the array is equal to 3
arr.some(function(element) { return element === 3; }); // returns true
What is the array method to determine if an array includes a specified value? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Determine if the array includes the value of 3
arr.includes(3); // returns true;
What is the array method to return the first index of a specified value within an array? Answer the following question: What does the method return if the value isn't found in the array? Provide example using the supplied code: arr = [3, 2, 3, 4, 5] Determine the first index of the value 3 Beginning the search at index 1, determine the first index of the value 3
arr.indexOf(3); // returns 0 arr.indexOf(3, 1); // returns 2
What is the array method to combine all elements of an array into a string? Hint: This methods accepts a string argument which is placed between each array element when the array is turned into a string. Answer the following question: Does this method mutate the array? Provide example using the supplied code: arr = ['JavaScript', 'totally', 'rocks'] Demonstrate combining all array elements into a comma-separated string Demonstrate combining all array elements into string with no separation betw…
Answer: It does not mutate the array. It instead returns a new string. arr.join() // returns "JavaScript,totally,rocks" arr.join('') // returns "JavaScripttotallyrocks" arr.join('-') // returns "JavaScript-totally-rocks"
How do you convert all arguments supplied to a function into an array of elements? Hint: There are three ways that can be used to solve this. Provide all 3 approaches in the example. Provide example using the supplied code: function convertArgumentsToArray() { return [Add code here] } convertArgumentsToArray(1, 2, 3); Convert all provided arguments to an array of elements
Array.from(arguments) // returns [1, 2, 3] Array.prototype.slice.call(arguments) // returns [1, 2, 3] [...arguments] // returns [1, 2, 3]
What is the array method to return the last index of a specified value within an array? Provide example using the supplied code: arr = [3, 2, 3, 4, 5, 3] Determine the last index of the value 3 Beginning the search at index 3, determine the last index of the value 3
arr.lastIndexOf(3); // returns 5 arr.lastIndexOf(3, 3); // returns 2
What is the array method to alter every element in an array? Answer the following question: Does this method mutate the array? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Double the value of every value in an array
Answer: It does not mutate the array. It instead returns a new array. arr.map(function(element){ return element * 2; }); // returns [2, 4, 6, 8, 10]
What is the array method to remove the last element in an array? Answer the following questions: Does this method mutate the array? What does the method return? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Remove the last element in the array
Answer: It does mutate the array. It returns the removed element arr.pop() // returns 5 arr // returns [1, 2, 3, 4]
What is the array method to add an element to the end of an array? Answer the following questions: Does this method mutate the array? What does the method return? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Add the value 10 to the end of the array
Answer: It does mutate the array. It returns the new number of elements in the array after the element has been added arr.push(10) // returns 6 arr // returns [1, 2, 3, 4, 5, 10]
What is the array method to apply a function against an accumulator and each element in an array (from left to right) to reduce it to a single value? Answer the following questions: Does this method mutate the array? Provide example using the supplied code: numbers = [1, 2, 3, 4, 5, 3, 7, 3] Add up all the values in the array Count the number of elements in an array that are of the value 3
Answer: It does not mutate the array. It instead returns a new string or number. numbers.reduce(function(sum, number) { return sum + number; }); // returns 28 numbers.reduce(function(count, value){ return value === 3 ? ++count : count; }, 0); // returns 3
What is the array method to apply a function against an accumulator and each element in an array (from right to left) to reduce it to a single value? Answer the following questions: Does this method mutate the array? Provide example using the supplied code: str = 'I am a sentence' Reverse a sentence word by word
Answer: It does not mutate the array. It instead returns a new string or number. str.split(' ').reduceRight(function(reversedString, word) { return `${reversedString} ${word}`; }); // returns "sentence a am I"
What is the array method to reverse an array? Answer the following questions: Does this method mutate the array? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Reverse the array
Answer: It does mutate the array. arr.reverse() // returns [5, 4, 3, 2, 1]
What is the array method to remove an element from the beginning of an array? Answer the following questions: Does this method mutate the array? What does the method return? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Remove the element from the beginning of the array
Answer: It does mutate the array. It returns the the element that was removed arr.shift() // returns 1 arr // returns [2, 3, 4, 5]
What is the array method to return a portion of an array? Answer the following questions: Does this method mutate the array? What does the method return? Is the end index included? Is it a shallow or deep copy? Provide example using the supplied code: arr = [1, 2, 3, 4, 5, 6, 7, 8] Return an array that begins at index 2 and includes all elements following it Return an array that begins at index 0 and ends at index 4 (including index 4) Return an array that begins at index 3 and ends at index 6…
Answer: It does not mutate the array. It instead returns a new array. It returns a new array with the element between the defined start and end index. No, it is not included. It is a shallow copy. arr.slice(2) // [3, 4, 5, 6, 7, 8] arr.slice(0, 5) // returns [1, 2, 3, 4, 5] arr.slice(3, 7) // returns [4, 5, 6, 7]
What is the array method to to sort an array? Answer the following questions: Does this method mutate the array? What does the method return? Provide example using the supplied code: var stringArray = ['Blue', 'Humpback', 'Beluga'] var numberArray = [40, 1, 5, 200] var objectsArray = [ { name: 'Zack' }, { name: 'Adam' }, { name: 'Jeff' } ] Sort an array of strings in ascending order Sort an array of strings in descending order Sort an array of numbers in ascending order Sort an array of numbers…
Answer: It does mutate the array. It returns the sorted array. stringArray.sort(); // returns ["Beluga", "Blue", "Humpback"] stringArray.sort().reverse() stringArray.sort((a, b) => b.localeCompare(a)) // returns ["Humpback", "Blue", "Beluga"] numberArray.sort((a, b) => { if (a > b) { return 1; } if (a < b) { return -1; } return 0; }) // returns [1, 5, 40, 200] numberArray.sort((a, b) => { if (b > a) { return 1; } if (b < a) { return -1; } return 0; }) // returns [200, 40, 5, 1] numberArray.sort…
What is the array method to add or remove elements to/from an array? Answer the following questions: Does this method mutate the array? What does the method return? Provide example using the supplied code: arr = [1, 2, 3, 4, 5, 6, 7, 8] Remove 2 elements beginning with index 3 Remove the last 3 elements Add the value 44 at the beginning of the array Remove 2 elements at index 5 and add the values 72 & 31
Answer: It does mutate the array. It returns a new array with any removed elements. arr.splice(3, 2) // returns [4, 5] arr // returns [1, 2, 3, 6, 7, 8] arr.splice(-3) // returns [6, 7, 8] arr // returns [1, 2, 3, 4, 5] arr.splice(0, 0, 44) // returns [] arr // returns [44, 1, 2, 3, 4, 5, 6, 7, 8] arr.splice(5, 2, 72, 31) // returns [6, 7] arr // returns [1, 2, 3, 4, 5, 72, 31, 8]
What is the array method to combine all elements of an array into a string? Hint: This method does not allow for an argument, which defines a string to be placed between each array element, when it turns the array into a string. Answer the following question: Does this method mutate the array? Provide example using the supplied code: arr = ['JavaScript', 'totally', 'rocks'] Combine all the elements of the array into a string
Answer: It does not mutate the array. It instead returns a new string. arr.toString() // returns "JavaScript,totally,rocks"
What is the array method to add an element to the beginning of an array? Answer the following questions: Does this method mutate the array? What does the method return? Provide example using the supplied code: arr = [1, 2, 3, 4, 5] Add the value 144 to the beginning of the array
Answer: It does mutate the array. It returns the new number of elements in the array after the element has been added arr.unshift(144) // returns 6 arr // returns [144, 1, 2, 3, 4, 5]