Book Image

Mastering Immutable.js

By : Adam Boduch
Book Image

Mastering Immutable.js

By: Adam Boduch

Overview of this book

Immutable.js is a JavaScript library that will improve the robustness and dependability of your larger JavaScript projects. All aspects of the Immutable.js framework are covered in this book, and common JavaScript situations are examined in a hands-on way so that you gain practical experience using Immutable.js that you can apply across your own JavaScript projects. The key to building robust JavaScript applications using immutability is to control how data flows through your application, and how the side-effects of these flows are managed. Many problems that are difficult to pinpoint in large codebases stem from data that’s been mutated where it shouldn’t have been. With immutable data, you rule out an entire class of bugs. Mastering Immutable.js takes a practical, hands-on approach throughout, and shows you the ins and outs of the Immutable.js framework so that you can confidently build successful and dependable JavaScript projects.
Table of Contents (23 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Removing duplicates


Sets aren't usually created directly. Instead, you convert lists of values that might have duplicates into sets to remove the duplicates.

Converting to sets

Assuming that you have a list that might contain duplicate values, you can convert the list to a set, as follows:

const myList = List.of(1, 1, 2, 2, 3, 3);
const mySet = myList.toSet();

console.log('myList', myList.toJS());
// -> myList [ 1, 1, 2, 2, 3, 3 ]
console.log('mySet', mySet.toJS());
// -> mySet [ 1, 2, 3 ]

As you can see, mySet has unique values from myList. The problem now is that you need to be able to use these unique values as part of an indexed collection. In other words, you need to convert the set back into a list.

Converting to sets, then back to lists

Once we've converted our list to a set, we'll have removed any duplicates. However, we do useful things with lists, such as getting values at a specific index or iterating over values in a consistent order. To do this, we'll convert our set back to...