arrays Flashcards
how do you :
access elements in an array
ex :
let shoppingList = [ ‘cheddar cheese’, ‘2% milk’];
arrayName [elementIndex]
ex : shoppingList [0] will return ‘cheddar cheese’
how do you :
update elements in an array (change the second item to whole milk)
ex :
let shoppingList = [ ‘cheddar cheese’, ‘2% milk’];
arrayName [elementIndex] = ‘newElement’;
ex :
shoppingList [1] = ‘Whole Milk’
console.log(shoppingList);
‘cheddar cheese’, ‘Whole Milk’
how do you :
add to the end of an array (add ‘ground beef’ to the end of the list)
ex :
let shoppingList = [ ‘cheddar cheese’, ‘Whole Milk’];
arrayName.push (elementName);
ex:
shoppingList.push (‘ground beef’)
console.log(shoppingList);
‘cheddar cheese’, ‘Whole Milk’, ‘ground beef’
*you can pass in / add multiple elements to the end
how do you :
add to the beginning of an array (add ‘salad’ to the beginning of the list)
ex :
let shoppingList = [ ‘cheddar cheese’, ‘Whole Milk’, ‘ground beef’];
arrayName.unshift (elementName);
ex:
shoppingList.unshift (‘salad’)
console.log(shoppingList);
‘salad’, ‘cheddar cheese’, ‘Whole Milk’, ‘ground beef’
*you can pass in / add multiple elements to the beginning
how do you :
remove from the end of an array
ex :
let shoppingList = [ ‘salad’, ‘cheddar cheese’, ‘Whole Milk’, ‘ground beef’];
arrayName.pop (elementName);
ex:
shoppingList.pop ( )
console.log(shoppingList);
‘salad’, ‘cheddar cheese’, ‘Whole Milk’
how do you :
remove from the beginning of an array
ex :
let shoppingList = [ ‘salad’, ‘cheddar cheese’, ‘Whole Milk’];
arrayName.shift (elementName);
ex:
shoppingList.shift ( )
console.log(shoppingList);
‘cheddar cheese’, ‘Whole Milk’
how do you :
merge two arrays
ex:
let fruits = [‘apple, ‘banana’];
let veggies = [‘asparagus’, ‘brussel sprouts’]
let array3 = array1.concat (array2);
ex :
fruits.concat (veggies);
console.log(fruits.concat(veggies));
[‘apple’, ‘banana’, ‘asparagus’, ‘brussel sprouts’]
*you can pass in / concat multiple arrays
how do you :
look for a value in an array, just looking for PRESENCE of an element (search for apples)
ex:
let fruits = [‘apple, ‘banana’];
arrayName.includes (elementName);
ex :
console.log(fruits.includes(‘apples’)); will return false — the fruits array includes ‘apple’ not ‘apples’
how do you :
copy a portion of an array AND store that portion in a new array (w/o editing the original)
- create a new array with just the ‘s’ animals
- create a new array starting at the first non-‘s’ to the end
ex:
let animals = [‘shark’, ‘salmon’, ‘salamander’, ‘turtle’, ‘lizard’, ‘bear’, ‘honey badger’];
let newArray = oldArray.slice (firstIndex, lastIndex + 1);
ex :
let s = animals.slice(0, 3)
console.log(s);
[‘shark’, ‘salmon’, ‘salamander’]
let nonS = animals.slice(3)
console.log(nonS);
[‘turtle’, ‘lizard’, ‘bear’, ‘honey badger’]
*can also use negative numbers – passing in -3 would return [‘lizard’, ‘bear’, ‘honey badger’]
how do you :
make a copy of an array w/o editing the original array (make a copy of animals)
ex:
let animals = [‘shark’, ‘salmon’, ‘salamander’, ‘turtle’, ‘lizard’, ‘bear’, ‘honey badger’];
let newArray = oldArray.slice( )
let copy = animals.slice( );
console.log(copy);
[‘shark’, ‘salmon’, ‘salamander’, ‘turtle’, ‘lizard’, ‘bear’, ‘honey badger’];
how do you :
modify an array by inserting/removing/replacing elements – describe the syntax
currentArray.splice (arg1, arg2, arg3);
arg1 = where you want to start removing or inserting (inclusive of the element itself)
arg2 = how many elements to delete, including/starting at the first argument (can be 0 if you’re only adding and don’t want to delete)
arg3 = (optional) what you want to add in – can be multiple values
explain the idea behind why arrays (and objects) can be mutable with a const declaration (use the example below) :
const myEggs = [‘brown’, ‘brown’];
myEggs.push(‘purple’);
myEggs[0] = ‘green’;
console.log(myEggs); // ['green', 'brown', 'purple']
… but you CAN’T set myEggs equal to another array with diff items
myEggs = [‘blue’, ‘pink’]; //NO!
contents can come and go (or be edited), but we want the reference to this array/object to remain the same (always pointing to this one object/array in memory, don’t want to reassign to something totally different)
(variables point to a REFERENCE of where an array is in memory, rather than the values in the array itself — so if we use keyword const, the [ ] space taken up in memory won’t change, but the elements inside can change)
how to make a copy of an array
arrayName.slice( )