Arrays: removing, inserting, and extracting elements Flashcards
What keyword removes the first element of an array?
shift
What keyword adds one or more elements to the beginning of an array?
unshift
What keyword inserts new elements anywhere in an array and/or removes elements from an array?
splice
What keyword copies elements from an array to another array?
slice
This statement created the array: var sizes = ["S", "M", "XL", "XXL", "XXXL"]; Insert "L" into the array between "M" and "XL"
sizes.splice(2, 0, “L”);
This statement created the array: var sizes = ["S", "M", "XL", "XXL", "XXXL"]; Copy the first 3 sizes of the array and put them into a new array, regSizes.
var regSizes = sizes.slice(0, 3);
Remove the first element of an array.
airlines.shift();
Add three number elements to the beginning of an array.
scores.unshift(0, 13, 5);
This statement created the array: var pets = ["dog", "cat", "ox", "duck", "frog"]; Add 2 elements after "dog" and remove "cat", "ox", and "duck".
pets.splice(1, 3, “fish”, “monkey”);
This statement created the array:
var pets = [“dog”, “cat”, “ox”, “duck”, “frog”];
Remove “cat” and “ox”.
pets.splice(1, 2);
This statement created the array:
var pets = [“dog”, “cat”, “ox”, “duck”, “frog”, “flea”];
Reduce it to “duck” and “frog” using slice.
pets = pets.slice(3, 5);