Variables, Types, and Collections - 23% Flashcards
array method - .sort
Sorts a-z by default w/no cb.
For numbers, need to pass cb:
For reverse, pass a cb that returns a neg num if the 1st is > the 2nd
ASC - use .sort( (a, b) => a - b)
DES - (or rev alphabetical), use .sort( a, b) => b - a)
array method - .forEach
Pass cb, the fn gets called for every element. Does not modify elements.
array1.forEach(element => console.log(element));
can also pass index (item, index)
array method - .map
Modify elements using a cb.
let modifiedArr = arr.map(function(element){
return element *3;
});
array methods - .pop, .shift
pop - removes last el of array and returns it
shift - removes first el of array and returns it
array methods - .splice, .slice
splice - removing, replacing, or adding els in specified positions. Modifies origin array.
slice - cuts out subset of array and returns it as a shallow copy, orig array is not modified
array methods - .push, .unshift
push - adds el to end of array
unshift - adds el to start of array
array methods - .every
Returns a Boolean value that represents whether all els pass the condition in the cb fn
array methods - .filter
Returns a shallow copy of a subset of the array, with only the els that pass the condition in the cb fn
array methods - .indexOf
Returns the first index at which a given el can be found, or -1
array methods - .reduce
Reduces the array to a single value by applying the cb to each el and passing the return to the next.
Takes cb w/2 args (accumulated val and the current val) and and initial value (0 below):
array1.reduce((acc, current) => acc + current, 0);
array methods - .reverse
Reverses the array in place, returns ref to same array
Data Types (8)
String, Number,
BigInt - create by appending n to a num, for numbers > 2^53-1
Boolean,
Null - nothing, empty, or value unknown
Undefined - variable is declared, but value is not assigned
Object - not primitive, used to store collections of data
Symbol - used to create unique identifiers for objects
Map object
collection of key/value pairs, the keys are unique
Const myMap = new Map( );
myMap.add(‘apple’, ‘red’);
myMap.get(‘apple’) // returns ‘red’
myMap.size( ) // returns 1
myMap.has(‘banana’) // returns false
myMap.delete(‘apple’) // removes apple/red pair from myMap
myMap.clear( ) // removes all pairs from myMap
Map obj v. reg Obj
Map - keys can be any type
Object - keys only strings or symbols
Map - easy to get size
Object - have to keep track manually
Map - iteration is insertion order
for (const [key, value] of myMap)
Set obj
Collection of values, similar to array, except all are unique.
Const mySet = new Set( );
mySet.add(‘orange’);
mySet.has(‘orange’) // returns true
mySet.size( ) // returns 1
mySet.delete(‘orange’) // removes orange from mySet
Can iterate in insertion order:
for (const item of mySet)
Set v Array
Set - can delete by value: mySet.delete(val);
Array - deletion by value is slow: (arr.splice(arr.indexOf(val), 1))
Array - value NaN cannot be found by indexOf
Set - only unique vals, don’t have to keep track of duplicates
Convert b/t Array and Set
Array.from(mySet) // creates array
[…mySet] // creates array
new Set(myArray); // creates set, removes duplicates