Transduce When the Collection Type is an Object

Paul Frend
InstructorPaul Frend
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

We've seen how we can transduce from arrays or other iterables, but plain objects aren't iterable in Javascript. In this lesson we'll modify our transduce() function so that it supports iterating from plain objects as well, treating each key value pair as an entry in the collection.

To do this we'll be using a lodash function called entries.

[00:00] If the collection we're transducing over is an object, our transduce function isn't going to work. The reason for this is object aren't iterables, so this for-of loop is just going to throw an error. To add support for plain object iteration, let's convert our objects to array pairs.

[00:17] Let's rename this collection argument, and then, before our loop, we'll create a variable called Collection. Here we want to check if our collection is a plain object. I'll call isPlainObject, which will take our collection and, if that's true, will call the Lodash helper entries with our collection, but otherwise will just return the collection.

[00:41] Entries is the same as the newer object.entries, but I'm running a version of Babel where that's not supported. That's why I'm using entries from Lodash. What it does is, let's say you have an object with keys and values, then after you call entries, what you're going to get back instead is an array where each element is another array, where the first element is the key and the other one is the value. That's why, after calling entries here, we'll still be able to iterate through our collection.

[01:14] To test this out, let's create our own version of object.values. We'll call this object Values. The point of this function is to take a source object and pour all of the values into an array. We'll use our Into helper. Our target will be an array. Our transformer should just pull out the values from the object. We'll call map. Here we'll get an array with a key and the value, and we just want to return the value, which is that index one. Our source will be the object.

[01:45] Let's try this out. If we call object Values with an object, we should get an array back with just our numbers. We got undefined as our result, so we've missed something. Since we're using brackets in our object values function, we haven't got an implicit return, so let's add that in. Now we get our array with our elements one and two.

egghead
egghead
~ 3 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today