JS Array Flashcards
Array.concat(array)
Combines both arrays and returns a new array without changing existing
When do async tasks is it better to use a forEach method or a for loop?
The for loop is better. forEach() is synchronous.
forEach()
synchronous method that will be used on all elements in an array
every()
works on elements in an array until it returns a falsy value
some()
loops through the array until the callback returns a truthy value
filter()
creates a new array with the callback values that returned truthy
map()
creates a new array from the values returned by the callback
reduce()
reduceRight()
builds up a value by repeatedly using the callback
reduceRight is in descending order RTL
needs an accumulator ‘sum’ | ‘total’
const total = numbers.reduce((sum, value) => { return sum + value; })
push() and pop()
used to add to the tail and remove elements from the tail of an array
How is performance for forEach() or reduce() versus a traditional for loop?
forEach() and reduce() are faster for small to medium datasets, for loop is faster for large datasets (500,000+)
.from()
creates and array from a string
Array.from(“string”)
.toString()
returns a string with the array values seperated by commas
shift() and unshift()
Shift dequeues and element from the head
unshift inserts an element at the head
.sort() and .reverse()
sorts and reverses an array
.slice()
returns selected elements of an array as a new array(inclusive, exclusive)