Data Structures Flashcards

1
Q

What are the 5 array methods and what do they do?

A

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

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

What is the difference between foreach and map?

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly