This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Differences between the Immutable.js Map() and List()

4:02 JavaScript lesson by

The Immutable.js Map() is analogous to a Javascript Object or Hash since it is comprised of key-value pairs. The Immutable.js List() is analogous to a Javascript Array and contains many of the same native methods. Let's compare the two and dive into the basics of List().

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

The Immutable.js Map() is analogous to a Javascript Object or Hash since it is comprised of key-value pairs. The Immutable.js List() is analogous to a Javascript Array and contains many of the same native methods. Let's compare the two and dive into the basics of List().

Avatar
Lee

Nice use of a rest operator for List.of, although this example could be simplified with Immutable.List(array) (i.e. no need to use the of function)

In reply to egghead.io
Avatar
Sean Inglis

Note that if you're using _.each to iterate over an array, there's no need to manually manage the index with a count; it's supplied as a second parameter to your iterated function (and the full array itself is provided as a 3rd).

Avatar
J.S.

Hey Sean and Lee! You are both right! I use those conventions in other videos. Not sure why I used the closure var count there... must have been a brain flub. :P Thanks for the feedback!

In reply to Sean Inglis

The immutable JS Map is analogous to a JavaScript object or hash, since it is comprised of key value pairs. The immutable JS List is analogous to a JavaScript Array and contains many of the same native methods. Let's compare the two and dive into the basics of List.

Here I've got the To Do class. We've been using this throughout the series and I've got our Add To Do to Map method, which we've been using throughout the series as well. We'll be writing an Add to To Do to List series to show the difference between how we add items to either a List or a Map.

This test here will show how we go about finding data within a List versus a Map. We've instantiated To Do and we're also instantiating a Map. Then we're adding the To Do to the Map. Let's see how this works with a List. We'll instantiate the List, make it empty. Then we'll take the List and we'll use our Add To Do to List method, do the same thing. We'll add the To Do to List via this method.

What do we do? We're going to return it by using the Push method. This is the same method you would find in Array. We'll say Push To Do, but the only difference is it returns a new immutable structure. It does not mutate the original To Dos. When we do that this new List will contain this To Do and we'll write an expectation to confirm that.

Let's first see if the To Dos Map contains the To Do. That should equal our To Do. The test passed. Let's write this for a List. How do we find an item in a List? It's going to be similar to an Array, but it's not going to use the actual bracket operators. Let me show you what I mean. To Do the List.git, because that's the common retrieval method in immutable JS, then we'll give it an Index, get (0), just like we would do with an Array. Now we can see if this is equal to the To Do, and it is.

Get is a little bit different, but Push is almost identical to Array and if we go over to the immutable JS documentation we can see how we create persistent changes in List. Lo and behold, we have Push, Pop, Shift, and a lot of the operators we're familiar with, with Array.

That's cool that we want to Push onto a List, but what if we want to instantiate a List with items already in it? We can do it a couple of different ways. We have an Array here. What we're going to do is I want to actually make this Array match this List. List will take a list of items just like Array, but it doesn't use the bracket operator. We just provide a List, which is kind of convenient. Then we can see if this matches our expectations.

Now let's loop through these two structures to see if they match. We're going to create account variable of 0. Then we're going to use lodash to iterate through the initial Array and it takes a function that will accept each item. Now we're just going to write a method that will compare the two. Expect our immutable List.git count to equal the item.

Then we also have to increment the count and now we have a passing test. There's also one other way to actually instantiate a List which is a little bit easier. If we have an existing Array, we can simply provide the Array by using the Rest operator. The Rest operator will take and break apart the Array into the items just like it was being added before.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?