Arrays Flashcards

1
Q

What is an array in JS?

Mutator methods, accessor methods, and iteration methods?

A

A list-like object

Mutator methods modify the array directly.
Accessor methods do NOT modify the array, but return some manipulated representation of it.
Iteration methods use a provided function when processing an array but they do not mutate the array; they may build a new array from the function’s results, or a boolean from testing each element, etc. Iteration methods take the .length prior to processing the array, so elements added to the array will not be processed.

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

Accessing array elements

A

Array indexes are object property names, which means they’re strings.

Can’t use dot-notation since starts with number:
a.0 //=> SyntaxError

Both work, since number is turned to string (same behavior w/ objects):
a[0] === a[‘0’]

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

.length property: data-type, default behavior, manipulation behavior

A

.length is a 32-bit unsigned integer

default behavior is to be +1 of the highest index in array:
  var a = [];
  a[6] = '.length is now === 7';

manually setting the .length to a lower value truncates the array (fastest too)

setting .length to a higher value does not add elements, just changes .length

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

Attributes

A
reference data type
has prototype & methods
#safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Methods:

Static (1), Mutators (6), Accessors (6), then Iterators (7)

A

Summary

Array.isArray —test if value is an array

.pop —removes & returns last array element

.push —adds values to end of array and returns new .length

.reverse —reverses array order

.shift —removes & returns first array element

.sort —sort array

.unshift —adds values to front of array and returns new .length

.concat —returns new array comprised of primary & argument arrays

.join —join array into a string

.slice —creates a new array by copying a slice of an existing array

.toString —returns string representation of array

.indexOf —return first index w/ matching value

.lastIndexOf —return last index w/ matching value

.forEach —call function on every element

.every —returns true if every iteration test returned true

.some —returns true if one iteration test returns true

.filter —returns new array of every element that satisfied iteration test

.map —returns a new array consisting of the return value of each iteration

.reduce —each iteration receives the value returned from the previous iteration, until the loop ends and the last returned value is what .reduce returns.

.reduceRight —same as .reduce but loops array from last to first

.concat is all of multiple arrays while .slice is a part of one array
.toString is .join w/o args, .join allows you to add glue
.indexOf & .lastIndexOf find an index
.forEach simple loops applying a function
.every tests all of the array while .some tests some of the array
.filter is some existing values while .map is all new values
.reduce is very flexible. anytime you’d like to generate a piece of data from looping an array, .reduce is your tool.

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