Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Make an Into Helper to Remove Boilerplate and Simplify our Transduce API


    Our transduce function is powerful but requires a lot of boilerplate. It would be nice if we had a way to transduce into arrays and objects without having to specify the inner reducer behaviour, i.e. how to build up values, since a given collection type will almost always have the same inner reducer.

    In this lesson we'll being doing just that with an into() helper function that will know if an array or object collection is passed in and handle each case accordingly.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: The purpose of into helper is to figure out which inner reducer to use based on the collection type of our output. We want to be able to call into with our target collection, our transform, and the source collection.

    Notice that we're not specifying this reducer anywhere. That's what we want into to figure out by itself. Let's try and create this. We'll define into, which will take to as our target, our transform, and our collection.

    Then in here, we just want to call transduce based on this collection type. We can start by checking if it's an array. If it is an array, then we'd want to call transduce with the transform, an array reducer, our target collection as our initial seed, and our collection as our source.

    If you recall, the push reducer just takes an accumulation and a value, the accumulation being our array, pushes on the value, and returns the accumulation. We also need to handle the case where our target collection is an object. Let's check for that.

    We're going use the isPlaying object helper from Lodash here, just because doing that ourselves is nontrivial. If it is, we'll again call transduce with our transform. In here, we'll need some object reducer. Our target collection will be our seed, and we'll pass the collection as our source.

    Finally, if neither of those conditions match, let's just throw an error. We'll say just into only supports arrays and objects as to. Now, we need to define this object reducer. Let's do that up here.

    That will take some object and a value. Let's treat this as a value is an object with keys and values, and we want those keys and values to be folded into this accumulation object. To accomplish that, let's just do a shallow merge.

    That should be everything we need. Let's start by testing it out on an array. I will call into with an array as our target. For our transform, let's create a simple operation which multiplies array values by 10, and then divides them by two.

    I'll call compose, and we want two map operations. The first one, dividing by two, and the second one, multiplying by 10. Let's make this a bit more readable. For our target collection, let's just use an array with a few numbers.

    Let's check our result. We get an array with 5, 10, 15, 20. Perfect. Let's also test our object support, and build up an object with matching keys and values from an array. We'll call into again. Our target is going to be an object. We want to transform to convert values into objects.

    That will be a map operation, which will take a value and return an object with both the key and the values as value from the array. Again, an array of numbers is our source. We can see we forget a return up here. Let's add that in.

    Now, let's give this a go. As expected, we get this object back, where the keys and values match each other. Let's also filter out any array values that aren't numbers. Let's say we've got the string hello in here, and a function returning the string, world.

    We don't get an error, since in JavaScript, we can actually have functions as keys. For our purpose here, we're just interested in the numbers. Let's add that into our transform. We'll add in a call to compose, and then I'm just going to filter to only include numbers.

    This works straight away, because we're already importing isNumber from Lodash. To recap, our into helper now lets us transduce into both arrays and objects without specifying our inner reducers.