Helpers Flashcards
How were we able to use helpers in the past?
Utility libraries like lodash
List all of the array helper methods in ES6
- forEach
- map
- filter
- find
- every
- some
- reduce
Being proficient in these will help with collections of data, the cornerstone of web development
What is the purpose of an array helper?
To help with iteration over an array
What is the forEach helper?
colors. forEach(function(color){
console. log(color);
});
function(color) is an iterator function, takes an element in the array, runs something then returns, repeats then with the next item in the array. Inside the iterator you can do whatever you want with the element/value
WHY? - Much less code logic on the screen and less error prone
What is a naming convention for forEach element callback? Assume the array name is plural
If the array name is plural, the callback function and the element argument should be singular
Say you have numbers.forEach(adder)…why would you not use adder()?
Because it will immediately call the function and then pass the result to forEach. Instead we want to hold the function for a future call with all array elements
What is a practical use of the forEach helper?
forEach helper is the swiss army knife of array helpers
What is the most widely used array helper?
The map helper
Why is the map array helper so special?
In large complex JS apps, you want to avoid mutating or changing data. Instead you want to return a new state.
For example, return to me double numbers of an existing array with numbers in the array.
Explain what’s going on here:
var doubled = numbers.map(function(number){
return number * 2;
})
Each number in the numbers array is being passed into the callback and is returning a result that is being placed in a new array. Therefore ‘doubled’ is pointing to a new array with the elements doubled.
Note the return statement is CRUCIAL!
What does it mean to pluck an array? What array helper helps with plucking?
Map
What is a practical example of using map array helper?
When dealing with like twitter, facebook, places where there is a lot of posts. Backend returns to you an array of objects that are posts and you need to map out the different values from the object
Explain how filter array helper works
We pass an element out of our source array and pass it into our iterator function. That iterator function has to return either a truthy or a false value if it returns. If truthy, it’ll be included in a result array so the array that gets returned from the filter helper.
JUST LIKE MAP DO NOT FORGET YOUR RETURN STATEMENT!!!
What is a practical example of when you want to use the filter array helper?
When you want to get comments associated with a post. Whenever you’re working on a client side applicatio you’ve got a list of records and you need to filter
var post = { id: 4, title: “New Post”};
var comments = [ {postId: 4, content: 'awesome post'}, {postId: 3, content: 'it was ok'}, {postId: 4, content: 'neat'} ];
function commentsForPost(post, comments){ return comments.filter(function(comment){ return comment.postId == post.id; }); }
commentsForPost(post, comments);
What is the purpose of the find array helper? How does it work?
To find a particular element in an array. Note: Once it finds one of the values it will immediately exit.
Again, the return statement is necessary
var posts = [ {id: 1, title: 'New Post'}, {id: 2, title: 'Old Post'} ];
var comment = { postId: 1, content: ‘Great Post’};
function postForComment(posts, comment){ return posts.find(function(post) { return post.id === comment.postId; }) }
postForComment(posts, comment);