Check out a free preview of the full Functional-Light JavaScript, v3 course:
The "Object Filter & Reduce Solution" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle live codes the solution to the exercise.

Get Unlimited Access Now

Transcript from the "Object Filter & Reduce Solution" Lesson

>> Kyle Simpson: All right, let's take a look at the solution to part one of this exercise. Again, the task was to implement the filter object and reduce object utilities alongside of map object. So we're going to take the same general approach, which is that we're definitely going to be producing a new object.

[00:00:19] And we need to know the list of the keys. So we're gonna say Object.keys of the passed in o. We are gonna loop over all,
>> Kyle Simpson: for (let key of keys). We're gonna loop over all of those properties in our passed in o. And we're going to pass them to a predicate function.

[00:00:43] So if the predicate function with the value o[key]. If that returns true, then we want for our new object to include that value and at that key position. So we wanna say newObj[key] = o[key]. And then after we've finished that loop, we know whatever is the contents of new object, we can simply return.

>> Kyle Simpson: We're gonna take a not terribly dissimilar approach with reduce object. Except here, because we're reducing agnostically, we don't even know what we're reducing to. But we're just gonna end up taking in an initial value and returning that result. So I'll start out my result equal to the initialValue.

[00:01:41] And then I'm going to run through all of the keys in this object just exactly like we did before. So I'll say var keys = Object.keys(o). We're going to run through all of those keys. And for each value in our object, we're gonna call the reducer function and get back the result.

[00:02:07] So for (let key of keys). And then we're going to say result =, calling the reducer function with the current result, whatever our current accumulator is, as well as the o[key]. And then we will return the result. So the difference here, though, is that we don't want to assume that we're gonna be producing a new object the way we do with our filter and our map utilities.

[00:02:43] We just want to assume that whatever our reducer function is returning to us, that's the thing that we'll be returning. Let's double-check to make sure that this is going to work correctly. I'm gonna copy some stuff over.
>> Kyle Simpson: And then we will copy this stuff.
>> Kyle Simpson: And then because I'm using a curry here, probably gonna need to implement that.

[00:03:21] So just to run this, just for our purposes.
>> Kyle Simpson: I'll say = (fn).
>> Kyle Simpson: Uh-oh, I didn't implement the isOdd, of course not. Okay, sorry about that, I didn't copy it over.
>> Kyle Simpson: Got these utilities down here.
>> Kyle Simpson: And there's our 38886, okay?