Array-Methods Flashcards
1
Q
isArray
A
Array.isArray(mixed value) => {bool} test #ES5
2
Q
pop
A
.pop() => {mixed} removes & returns last array element; if array is empty, returns undefined #mutator #safe
3
Q
push
A
.push(mixed value1, ... ) => {num} new .length after adding values to end of array #mutator #safe
4
Q
reverse
A
.reverse() => {arr-ref} reverses order of array, then returns a reference to itself #mutator #safe
5
Q
shift
A
.shift() => removes & returns first array element; or returns undefined if array is empty #mutator #safe
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)
7
Q
unshift
A
.unshift(mixed value1, ... ) => {num} new .length after adding values to front of array #mutator #safe
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]]
9
Q
join
A
.join(str glue=',') => {str} array glued together into string #accessor #safe
glue
- if not string, converted into one
10
Q
slice
A
.slice(num start=0, num end=length) => #accessor #safe #shallow-copy
start/end
- if < 0, s/e=length+s/e
11
Q
toString
A
.toString() => {str} array in string form; identical to .join() #accessor #safe - but ES5 uses .join() instead of .toString()
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)
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
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
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