Data Structures Flashcards
What are the 5 array methods and what do they do?
map - map creates a new copy of the original array and performs a function on each element in the array
filter - filter creates a new array with elements that meet the given condition(s)
find - find selects a single element in the array
forEach - does NOT create a new array, performs a function on each element in the array
reduce - does NOT create a new array, used to produce a single value from multiple elements in the array e.g. Sum of all elements
I’m an array mfffr
What is the difference between foreach and map?
1) Map returns a new Array, but forEach doesn’t.
2) map can do method chaining, foreach can’t
Both map and forEach don’t mutate (change) the original array.
let data = arr.forEach((num) => (num ++)) console.log(data) // undefined
let data = arr.map((num) => (num ++)) console.log(data) // [1, 4, 3, 8, 5, 12, 7]