Array Obj properties and methods Flashcards
Review
what do the methods arr.push() and arr.unshift do? explain the syntax and parameters
They add things to the end or beginning of an array respectively
what do the methods arrVar.pop() and arrVar.shift() do? explain the syntax and parameters
used to remove elements to end or front of array.
What does the method arrVar.fill() do?explain the syntax and parameters
fill an array with a given value, can include a range
What does the method arrVar.reverse()? explain the syntax and parametersdo?
used to reverse the order of an array.
What does the method arrVar.sort() do? explain the syntax and parameters what is the default sort order?
used to sort arrays in place. insert (a,b)=>a-b for acc order. remember if it starts with a it’s accending.
How do we implement destructing, give an example with proper syntax
let [a, b, …rest] = [10, 20, 30, 40, 50];
console.log(rest); // expected output: [30,40,50]
What does the method arrVar.slice() do? Give an alternative way for the same effect, also what is the parameter syntax
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.
What does the method arrVar.indexof() of arrVar.lastIndexOf() do? What are the parameters taken.
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).
What does the method arrVar.includes() do?explain the syntax and parameters
used to check an array (or string) to see if ti includes a certain value. returns true or false.
What does the method arrVar.join() do?explain the syntax and parameters
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.
What does the method arrVar.splice() do? explain the syntax and parameters
Adds and/or removes element(s) from an array.
arr.splice(beginIndex,HowmanyToDelete,)
What are the properties of an array object? explain the syntax and what it does.
used to check the length of an array.
What does the method arrVar.every() do? explain the syntax and parameters
used to check every element in array against a statement, returns true if all pass, otherwise returns false.
arrVar.every((x)=>x>3)
What does the method arr.some() do? explain the syntax and parameters
used to potentially check every element in array against a statement, returns true if one passes, otherwise returns false.
arr.some((x)=>x>3)
What does the method arrVar.filter() do? explain the syntax and parameters
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)
What does the method arrVar.find() do? explain the syntax and parameters
used to find the first element of an array that passes the statement. returns element .
arrVar.find((x)=>x>3)
What does the method arrVar.forEach() do? explain the syntax and parameters
used to iterate through an array and execute a command (callback) but not the map one.
What does the method arrVar.map() do? explain the syntax and parameters
used to iterate through each value in an array and execute a callback… can mutate the array. arrVar.map((x,i)=>x*2)
What does the method arrVar.reduce() & arr.reduceRight() do? explain the syntax and parameters
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);
What are the properties of an array object? explain the syntax and what it does.
used to check the length of an array.
arr.length;
What does the method arrVar.findIndex() do? Explain the syntax
used to find the first element of an array that passes the statement. returns element index.
arrVar.findIndex((x)=>x>3)
How can we make an array from an a array like? What types do those include and what is the styntax? also
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));
What does the toString method do on an array?
The toString() method returns a string representing the specified array and its elements.
console.log(array1.toString()); // expected output: "1,2,a,1a"
What does the values method do on an array?
How can we use it and what is the syntax?
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
How do we find the length of an array?
arr.length
How do we check is a variable is an array?
Array.isArray(var);
Array.isArray([1,2]);
How do we remove an object from the middle of an array?
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
What are some of the easiest ways to make the .map .filter .reduce functions that more powerful?
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);
What are the properties methods that we can use on both arrays and strings?
syntax, parameters and expected results?
includes(string) slice(firstindex(included),lastindex(excluded)) indexOf() lastIndexOf() .length .concat();
.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?
.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