Immutable.Set.contains returns false

I want to create an immutable set of paths. The path, in my case, is just an array of strings. So, let's say we have the following ways.

var paths = [["a"], ["a", "b", "c"]]; 

Then I create an immutable set like this

 var selectedPaths = Immutable.Set(paths); 

Although selectedPaths.first() returns ["a"] , I cannot understand why selectedPaths.contains(["a"]) returns false .

EDIT : Well, I got an answer why this happens, but I still can't get it to work the way I need.

SOLUTION As @Alnitak stated, I solved this by comparing the path to Immutable.List(["a"]) instead of a simple array

+5
source share
2 answers

According to the docs, Immutable uses the Immutable.is() function to perform equality checks, but the .is() check only performs a “comparison of values” "checks when other Immutable.* Objects are Immutable.* , and not the native JS arrays for which it performs link comparison check.

So try to save your internal values ​​as Immutable.List instead of a simple JS array.

+1
source

Testing two different arrays for equality in this way does not work in Javascript, for example.

 ["a"] == ["a"] // returns false 

I assume that the Immutable JS library does a simple equality check. Unfortunately, you will need to perform a more extensive check yourself.

0
source

Source: https://habr.com/ru/post/1241427/


All Articles