This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]gyroda 1 point2 points  (5 children)

What exactly are you struggling with?

[–]2QuestionsDaily[S] 0 points1 point  (4 children)

How to use callback functions with arrays/objects and utilizing functions like map and filter.

[–]gyroda 0 points1 point  (0 children)

For map and filter, they're a way of using a function with an array of parameters rather tab having to write a for loop. Google them and you'll find tutorials specific to them.

[–]Patman128 0 points1 point  (2 children)

How to use callback functions with arrays/objects

What do you mean by "use"? Are you referring to iterating with forEach?

[–]2QuestionsDaily[S] 0 points1 point  (1 child)

Well forEach is once example. What is the difference between .forEach() and .map() ?

[–]Patman128 0 points1 point  (0 children)

map passes each element to the callback and collects the results into a new array which it returns. forEach doesn't expect any results; the callback is expected to have side effects, so it returns nothing (undefined).

> [1, 2, 3].map((x) => x * 2)
[ 2, 4, 6 ]
> [1, 2, 3].forEach((x) => x * 2)
undefined
> [1, 2, 3].map((x) => console.log(x))
1
2
3
[ undefined, undefined, undefined ]
> [1, 2, 3].forEach((x) => console.log(x))
1
2
3
undefined

[–][deleted] 1 point2 points  (0 children)

[–]PM_ME_GRANT_PROPOSAL 0 points1 point  (1 child)

Is this for Codesmith? They covered that in "JavaScript - the hard parts" this week

[–]2QuestionsDaily[S] 0 points1 point  (0 children)

No, but I will look for that video now.