Iterating Over an Immutable.js Map()

J.S. Leonard
InstructorJ.S. Leonard

Share this video with your friends

Send Tweet

Immutable.js provides several methods to iterate over an Immutable.Map(). These also apply to the other immutable structures found within the Immutable.js family, such as Set and List. The primary methods are map and forEach, but we will also cover filter and groupBy.

Sandeep S Kumar
Sandeep S Kumar
~ 5 years ago

"return" is not required in the first line of markAllTodosAsComplete as we're not assigning the returned value back to todos. I know it doesn't matter but might avoid some confusion.

Ilyes
Ilyes
~ 5 years ago

You could also use map instead of foreach:

function markAllTodosAsComplete(todos) {
    return todos.map(todo => {
        todo.completed = true;
        return todo;
    });
}
Vlado Tesanovic
Vlado Tesanovic
~ 5 years ago

Function addTodo does not add elements in right order. In your example, you use getTodoTexts, and you are expecting text that is same on all objects "I'm a todo!", but try with to change getTodoTexts to return title instead of text, and try to fetch .first() element then expect "Todo0", it will fail and you will see that is not consistent.

J.S. Leonard
J.S. Leonardinstructor
~ 5 years ago

Hi Vlado!

That's correct and expected. Immutable.Map() is unordered. If you need want order (at the cost of some performance) use Immutable.OrderedMap(). Here's a link to the docs: https://facebook.github.io/immutable-js/docs/#/OrderedMap

Thanks! Leonard