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

Finding collection values


Filtering collections is a good idea when you're not exactly sure what you'll find. At other times, you know that there's exactly one collection value needed. The problem with filtering is that the entire collection is searched no matter what. When you try to find a single item, the search ends with the first match.

Value existence checks

There are different types of existence checks that you can perform, depending on the type of collection. If it's a list, it's an indexed collection, which means that you can check for the existence of a specific index. If it's a map, it's a keyed collection, which means that you can check for the existence of a specific key. You can also check for the existence of a value in either collection type.

Let's create a list and check for some indexes, as follows:

const myList = List.of(1, 2, 3);
const myListHas2 = myList.has(2);
const myListHas3 = myList.has(3);

console.log('myListHas2', myListHas2);
// -> myListHas2 true
console.log...