Array Obj properties and methods Flashcards

Review

1
Q

what do the methods arr.push() and arr.unshift do? explain the syntax and parameters

A

They add things to the end or beginning of an array respectively

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

what do the methods arrVar.pop() and arrVar.shift() do? explain the syntax and parameters

A

used to remove elements to end or front of array.

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

What does the method arrVar.fill() do?explain the syntax and parameters

A

fill an array with a given value, can include a range

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

What does the method arrVar.reverse()? explain the syntax and parametersdo?

A

used to reverse the order of an array.

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

What does the method arrVar.sort() do? explain the syntax and parameters what is the default sort order?

A

used to sort arrays in place. insert (a,b)=>a-b for acc order. remember if it starts with a it’s accending.

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

How do we implement destructing, give an example with proper syntax

A

let [a, b, …rest] = [10, 20, 30, 40, 50];

console.log(rest); // expected output: [30,40,50]

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

What does the method arrVar.slice() do? Give an alternative way for the same effect, also what is the parameter syntax

A

use arr.slice() or […arrVar] used to copy an existing array.

Slice’s syntax is arr.slice(begin index for slice,endindex for slice(excluded);

We can use negative numbers in slice to start from the back of an array and just copy the last item arr.slice(-1) if ther eis only one parameter passed this will return from the passed index to the end of the array.

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

What does the method arrVar.indexof() of arrVar.lastIndexOf() do? What are the parameters taken.

A

used to return the first/last index of an value in an array or returns -1. takes a second option parameter for the index of where to start the search (included).

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

What does the method arrVar.includes() do?explain the syntax and parameters

A

used to check an array (or string) to see if ti includes a certain value. returns true or false.

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

What does the method arrVar.join() do?explain the syntax and parameters

A

used to join elements of an array… can also use arr.concat(arr2) or we can use the spread operator return […arr,…arr2] arrVar.toString() can be used to return it as well.

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

What does the method arrVar.splice() do? explain the syntax and parameters

A

Adds and/or removes element(s) from an array.

arr.splice(beginIndex,HowmanyToDelete,)

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

What are the properties of an array object? explain the syntax and what it does.

A

used to check the length of an array.

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

What does the method arrVar.every() do? explain the syntax and parameters

A

used to check every element in array against a statement, returns true if all pass, otherwise returns false.

arrVar.every((x)=>x>3)

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

What does the method arr.some() do? explain the syntax and parameters

A

used to potentially check every element in array against a statement, returns true if one passes, otherwise returns false.

arr.some((x)=>x>3)

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

What does the method arrVar.filter() do? explain the syntax and parameters

A

used to create a new array with all elements that pass the test. ex. all num above 10 & all str that pass /^\w/ regex.test.

arrVar.filter((x)=>x>3)

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

What does the method arrVar.find() do? explain the syntax and parameters

A

used to find the first element of an array that passes the statement. returns element .

arrVar.find((x)=>x>3)

17
Q

What does the method arrVar.forEach() do? explain the syntax and parameters

A

used to iterate through an array and execute a command (callback) but not the map one.

18
Q

What does the method arrVar.map() do? explain the syntax and parameters

A

used to iterate through each value in an array and execute a callback… can mutate the array. arrVar.map((x,i)=>x*2)

19
Q

What does the method arrVar.reduce() & arr.reduceRight() do? explain the syntax and parameters

A

used to apply function (usually a math using an accumulator and a current value to reduce the array to a single value.

arrVar.reduce((a,b)=>a+b,0);

20
Q

What are the properties of an array object? explain the syntax and what it does.

A

used to check the length of an array.

arr.length;

21
Q

What does the method arrVar.findIndex() do? Explain the syntax

A

used to find the first element of an array that passes the statement. returns element index.

arrVar.findIndex((x)=>x>3)

22
Q

How can we make an array from an a array like? What types do those include and what is the styntax? also

A

The method is .from called on the Array object, it also can take a callback which mutates each element of the array.

console.log(Array.from([1, 2, 3], x => x + x));

23
Q

What does the toString method do on an array?

A

The toString() method returns a string representing the specified array and its elements.

console.log(array1.toString());
// expected output: "1,2,a,1a"
24
Q

What does the values method do on an array?

How can we use it and what is the syntax?

A

The values() method returns a new Array Iterator object that contains the values for each index in the array.

const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
console.log(value); // expected output: “a” “b” “c”
}

of iterate like

var a = ['a', 'b', 'c', 'd', 'e']; 
var iterator = a.values();

console. log(iterator.next().value); // a
console. log(iterator.next().value); // b

25
Q

How do we find the length of an array?

A

arr.length

26
Q

How do we check is a variable is an array?

A

Array.isArray(var);

Array.isArray([1,2]);

27
Q

How do we remove an object from the middle of an array?

A

use a combinations of slices or concat while exempting the index to leave out..

OR use something like the filter method with the index of the item to remove as the conditional. EX arr.filter((a,b,i)=>if(i==Math.floor(arr.length/2)))a+b:a,0

OR splice can be used to indirectly remove an item

28
Q

What are some of the easiest ways to make the .map .filter .reduce functions that more powerful?

A

Use conditionals such as && or ||, also remember that the arrow functions automatically return the statement, so for more complex functions where we want to take multiple actions use a function instead and you can list actions separately like i++; return i*2; instead of having to do it all in one line.

so for filter the syntax is arr.filter((a,b,i)=>i==0?a+b:a,0);

29
Q

What are the properties methods that we can use on both arrays and strings?

syntax, parameters and expected results?

A
includes(string)
slice(firstindex(included),lastindex(excluded))
indexOf()
lastIndexOf()
.length
.concat();
30
Q

.map() .reduce() .filter() .some() .every() .find() .splice() includes() what are they good for?

What should we use to mutate?

What should we use to remove?

What should we use to add?

test truth?

finding a certain value?

A

.map is good if we need to mutate each array, maybe combo with a filter if we want to mark array indexes to remove by replacing it with a value we can filter out after would be nice.

reduce is meant for cases where we need to sum of things in an array..

filter is good for either removing an item at a certain index or removing a multitude with correct if statements.

.some is good for just that checking if at least one returns true can make exceptions with an if statement.

every is the same but remember we can use if statements to make some exceptions so everything comes out true or false.

.find is good for finding things that past 1 or multiple conditions.

includes is like .test() for arrays is just the same good for checking if a regex includes x of an array really good to replace a long if x= y or x=z. or we can use test. for strings. maybe a while statement of while(arr.includes(‘x’)) then filter can be very powerful