Bookcliff / learning

2 stars 0 forks source link

Learn about .map, .forEach, .filter #4

Closed Bookcliff closed 2 years ago

Bookcliff commented 2 years ago

Do this after #10. Learn about

This is the main way you'll be iterating through arrays. You can close this issue when you can answer the following questions:

RusseII commented 2 years ago

Use arr.forEach when you need to iterate over an array, map, or set and apply a callback function to each element of that object.

Yep, correct! Also - It's almost always arrays. I can't remember the last time I used a set or a map data type.

arr.forEach is a more semantic and intuitive method of iterating over an array than a for() loop.

Yep!

so if you are optimizing speed it may be more helpful to use a for() loop in that specific case?

Probably not. Over optimizations like that are almost never worth it. There are some niche use cases to use a regular for loop with async code. We'll talk more about that after you learn about promises

Use .map whenever you want to create a new array from an existing array. .map does not change the existing array.

Yep exactly. One of the most common noob mistakes is using .map when they should be using .forEach