Basic Data Structure Flashcards

1
Q

The key difference between pop() and shift() and their cousins push() and unshift(),

A

is that neither method takes parameters, and each only allows an array to be modified by a single element at a time

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

what if we want to remove an element from somewhere in the middle? Or remove more than one element at once?

A

splice() allows us to do just that: remove any number of consecutive elements from anywhere in an array.

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

splice() can take up to ___ parameters, and the ___ can be used to _____

A

3, third, add to the array

const numbers = [10, 11, 12, 12, 15];
const startIndex = 3;
const amountToDelete = 1;

numbers.splice(startIndex, amountToDelete, 13, 14);
// the second entry of 12 is removed, and we add 13 and 14 at the same index
console.log(numbers);
// returns [ 10, 11, 12, 13, 14, 15 ]

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

use splice() with 3 parameters for…

A

quickly switching out an element, or a set of elements, for another.

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

what slice() does

A

rather than modifying an array, slice() copies, or extracts, a given number of elements to a new array, leaving the array it is called upon untouched

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

using slice()

A

slice() takes 2 arguments: (1) element number to start from, (2) element number to stop at (exclusive)

slice(2,4) will take elements 3 and 4

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

we can use the spread operator to copy an array like so (code):

A
let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what spread operator can do to string multiple elements from arrays in various combinations (code)

A

let thisArray = [‘sage’, ‘rosemary’, ‘parsley’, ‘thyme’];

let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
// thatArray now equals ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Check For The Presence of an Element With

A

indexof()

fruits. indexOf(‘oranges’); // returns 2
fruits. indexOf(‘pears’); // returns 1, the first index at which the element exists

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

what indexof() returns if the element doesn’t exist in an array

A

let fruits = [‘apples’, ‘pears’, ‘oranges’, ‘peaches’, ‘pears’];

fruits.indexOf(‘dates’); // returns -1

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

remove a key-value pair from an object

A

delete obj.key;

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

what if we just wanted to know if an object has a specific property?

A

two different ways to do this:

hasOwnProperty()
the in keyword

users.hasOwnProperty(‘Alan’);
‘Alan’ in users;

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

Objects ____ maintain an ordering to stored keys like arrays do; thus a key’s position on an object, or the relative order in which it appears, is ____ when referencing or accessing that key.

A

do not, irrelevant

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

Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a ____ statement.

A

for…in

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

Example of for…in statement (code)

A

for (let user in users) {
console.log(user);
}

// logs:
Alan
Jeff
Sarah
Ryan
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

generate an array which contains all the keys stored in an object using ____

A

the Object.keys() method

17
Q

Object.keys() accepts ____ as argument

A

an object

18
Q

Object.keys(obj) will return _____. Again, there will be ______ to the entries in the array.

A

an array with strings representing each property in the object, no specific order

19
Q

Object.assign() does…

A

takes a target object and source objects and maps properties from the source to target

commonly used to make shallow copies of objects
const newObject = Object.assign({}, obj1, obj2);