Array-Methods Flashcards

1
Q

isArray

A
Array.isArray(mixed value)
=>  {bool} test
#ES5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

pop

A
.pop()
=>  {mixed} removes & returns last array element; if array is empty, returns undefined
#mutator
#safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

push

A
.push(mixed value1, ... )
=>  {num} new .length after adding values to end of array
#mutator
#safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

reverse

A
.reverse()
=>  {arr-ref} reverses order of array, then returns a reference to itself
#mutator
#safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

shift

A
.shift()
=>  removes & returns first array element; or returns undefined if array is empty
#mutator
#safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

sort

A
.sort(func compare=unicode-point-values)
=>  {arr-ref} sorts array, then returns reference to itself
#mutator
#safe

compare

  • arguments (a, b)
    • if > 0 returned, a > b
    • if < 0 returned, a < b
    • if 0 returned, a == b
  • if omitted, converts every element into a string & uses Unicode point values (16 bit number representing character)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

unshift

A
.unshift(mixed value1, ... )
=>  {num} new .length after adding values to front of array
#mutator
#safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

concat

A
.concat(mixed value1, ...)
=>  {arr} new array from primary + value1 + ...
#accessor
#safe
#shallow-copy - meaning reference types have their references copied, so both old and new arrays point to same reference

value
- single values or array becomes the same dimension:
[1, 2].concat([3, 4], 5, [[6]]) => [1, 2, 3, 4, 5, [6]]

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

join

A
.join(str glue=',')
=>  {str} array glued together into string
#accessor
#safe

glue
- if not string, converted into one

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

slice

A
.slice(num start=0, num end=length)
=>  
#accessor
#safe
#shallow-copy

start/end
- if < 0, s/e=length+s/e

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

toString

A
.toString()
=>  {str} array in string form; identical to .join()
#accessor
#safe - but ES5 uses .join() instead of .toString()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

indexOf

A
.indexOf(mixed searchValue, num start=0)
=>  {num} first index w/ matching value, else -1
#accessor
#ES5

start

  • if < 0, start=length+start
  • if > length, returns -1 (not found)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

lastIndexOf

A
.lastIndexOf(mixed searchValue, num start=length)
=>  {num} last index w/ matching value, else -1
#accessor
#ES5

start

  • the array is search backwards, so if >= length, whole array is searched
  • if < 0, start=length+start (offset @ end of array), so -2 would skip thelast 2 elements in the array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

forEach

A
.forEach(func iter, obj this=default-this)
=>  {undefined}
#iterator
#ES5
# note - you can't break a .forEach loop, so use .some(return true) or .every(return false) if breaking is needed.

iter
- args: value, index, array

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

every

A
.every(func iter, obj this=default-this)
=>  {bool} iterates each element until false is returned, causing .every to return false, else returns true.
#iterator
#ES5

iter
- args: value, index, array

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

some

A
.some(func iter, obj this=default-this)
=>  {bool} iterates each element until true is returned, causing .some to return true, else returns false.
#iterator
#ES5

iter
- args: value, index, array

17
Q

filter

A
. filter(func iter, obj this=default-this)
=>  {arr} creates a new array containing the elements who's iteration returned true
#iterator
#ES5

iter
- args: value, index, array

18
Q

map

A
.map(func iter, obj this=default-this)
=>  {arr} creates a new array containing the *values* returned from each iteration
#iteration
#ES5
#note - iteration called on element when value !== undefined

iter
- args: value, index, array

19
Q

reduce

A
.reduce(func iter, mixed initial-value)
=>  {mixed} each iteration receives the value returned from the previous iteration, until the loop ends and the last returned value is what .reduce returns.
#iterator
#ES5

iter
- args: prevValue, arrValue, index, arr

initial-value !! READ CAREFULLY !!

  • if you do NOT provide an initial value, the first iteration will be the first element as prevValue and the second element as arrValue
    • it makes sense if you’re trying to pluck a single value from an array, which you’d usually initialize as the first value
20
Q

reduceRight

A
.reduceRight(func iter, mixed initial-value)
=>  {mixed} same as .reduce but loops from last to first
#iterator
#ES5