6

I want to create an Immutable Set of paths. A path, in my case, is just an array of strings. So let's say we have the following paths.

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

I then create the 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 as to why this is happening, but I still cannot get it to work as I need it to.

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

XeniaSis
  • 2,192
  • 5
  • 24
  • 39

2 Answers2

2

According to the docs, Immutable uses the Immutable.is() function to perform equality checks, but that .is() check only performs "value comparison" checks when given other Immutable.* objects, and not native JS arrays, for which it performs a "reference comparison" check.

Therefore, try storing your inner values as an Immutable.List instead of as a plain JS array.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

Testing two different arrays for equality this way doesn't work in Javascript, e.g.

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

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

Steven Bakhtiari
  • 3,227
  • 2
  • 20
  • 24
  • so, what should I do to get the desired response? – XeniaSis Jan 25 '16 at 15:11
  • See my edit - you'll need to perform your equality check manually. I'm not sure exactly what you want to do, but if you want to reduce the set, you could run a filter operation and provide your own function to do the comparison. See this SO question that focuses on array equality checks in JS: http://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript – Steven Bakhtiari Jan 25 '16 at 15:14