arrays Flashcards

1
Q

how do you :
access elements in an array

ex :
let shoppingList = [ ‘cheddar cheese’, ‘2% milk’];

A

arrayName [elementIndex]

ex : shoppingList [0] will return ‘cheddar cheese’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how do you :
update elements in an array (change the second item to whole milk)

ex :
let shoppingList = [ ‘cheddar cheese’, ‘2% milk’];

A

arrayName [elementIndex] = ‘newElement’;

ex :
shoppingList [1] = ‘Whole Milk’

console.log(shoppingList);

‘cheddar cheese’, ‘Whole Milk’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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’];

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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’];

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do you :
remove from the end of an array

ex :
let shoppingList = [ ‘salad’, ‘cheddar cheese’, ‘Whole Milk’, ‘ground beef’];

A

arrayName.pop (elementName);

ex:
shoppingList.pop ( )

console.log(shoppingList);

‘salad’, ‘cheddar cheese’, ‘Whole Milk’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how do you :
remove from the beginning of an array

ex :
let shoppingList = [ ‘salad’, ‘cheddar cheese’, ‘Whole Milk’];

A

arrayName.shift (elementName);

ex:
shoppingList.shift ( )

console.log(shoppingList);

‘cheddar cheese’, ‘Whole Milk’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how do you :
merge two arrays

ex:
let fruits = [‘apple, ‘banana’];

let veggies = [‘asparagus’, ‘brussel sprouts’]

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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’];

A

arrayName.includes (elementName);

ex :
console.log(fruits.includes(‘apples’)); will return false — the fruits array includes ‘apple’ not ‘apples’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do you :
copy a portion of an array AND store that portion in a new array (w/o editing the original)

  1. create a new array with just the ‘s’ animals
  2. create a new array starting at the first non-‘s’ to the end

ex:
let animals = [‘shark’, ‘salmon’, ‘salamander’, ‘turtle’, ‘lizard’, ‘bear’, ‘honey badger’];

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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’];

A

let newArray = oldArray.slice( )

let copy = animals.slice( );

console.log(copy);

[‘shark’, ‘salmon’, ‘salamander’, ‘turtle’, ‘lizard’, ‘bear’, ‘honey badger’];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how do you :

modify an array by inserting/removing/replacing elements – describe the syntax

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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!

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how to make a copy of an array

A

arrayName.slice( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly