Arrayology Flashcards
Add element to end of array
array.push(element)
Remove and return element from end of array
array.pop(element)
Add element to beginning of array
array.unshift(element)
Remove and return element from beginning of array
array.shift(element)
Find the first instance of a value
array.indexOf(value)
Optional second argument specifies where to start searching from.
Find the last instance of a value
array.lastIndexOf(value)
Optional second argument specifies where to start searching from.
Get me a copy of the elements between some indices
array.slice(start, stop)
Start is inclusive, stop exclusive. If either is omitted, the slice starts at 0 or ends at the last element, respectively.
String slicing works the same.
How do I glue two arrays together
array.concat(other_array)
String indexing??
string. indexOf(substring)
- -> Note: the argument for indexOf may be a whole substring, and indexOf will return the index of the start of the substring.
How can I remove whitespace from a string?
string.trim()
What about the length of an array or object or string?
array. length
string. length
object. length
Reverse an array in place?
array.reverse()
Apply a function to each element of an array?
array.forEach(function)
NOTE: the function may be user-defined, may even be defined on the spot in the call to forEach.
Sift through the elements of an array?
array.filter(function) –> returns a new array with the elements of the original array for which the function argument returns true.
Transform an array?
array.map(function) –> returns a new array created by applying the function to each element of the original.