Javascript 30 (Day 4)

Array Cardio Day 1

Usmaan Ali
CloudBoost

--

Demo can be found here.

What I learned on this mini-project.

filter

This can be used to assess each item of an array against a condition, and create a new one for those that pass.

Here, we used a more concise syntax. If the resulting boolean expression returns true, then it will 'pass the condition' and be returned in the filtered array.

  • inventor takes the role of the current iterand, much like i in a traditional for (var i = 0; i++; i < arr.length) loop

Array.from

This is useful when you wish to apply array methods to a node list, that might have been extracted from, say a website (in this project the node list was taken from wikipedia).

Now, you are able to apply, map, reduce, sort etc... to links.

reduce

This method takes an array and attempts to whittle it down to single value (or values shown in the next example).

It’s best to think of the parameters total and inventor as an accumulator and iterand. The 0 provides a starting point for the total, since if it were omitted, it would use undefined to add the first iterand to.

reduce (part 2)

Perhaps a more complex use case for reduce, is trying to reduce multiple elements in an array.

Here, the goal was to reduce each element to the number of occurrences in the given array.

The key in this case, was to use an empty object as the initial value for the object, and call obj[key]++ for each iteration.

Originally published at gist.github.com.

--

--