Search code examples
javascriptimmutable.js

Does immutablejs allow searching with a reference type


I have a piece of code that looks like this

const { Map } = require('[email protected]')

let map = new Map().setIn(["user",["alex","country"]],"Poland")
map.getIn(["user",["alex","country"])

I hope the above would return "Poland", but such is not the case. I presume the reference type ["alex", "country"] is serialized differently when setting and querying. Any assistance on how to achieve this would be helpful. I shy away from having to handle the Array serialization manually


Solution

  • The array isn't serialized. immutable uses the actual object (its reference) as a key and the equal operator to compare keys. That means that the first ["alex","country"] and the second ["alex","country"] are two different keys.

    This snippet shows the problem:

    if (['alex', 'country'] !== ['alex', 'country']) {
      console.log('The references are different');
    }

    I have 3 solutions:

    1. Store a reference in a variable

    You can store a reference to the array in a variable to use it as a key:

    const { Map } = require('immutable');
    
    const arr = ['alex', 'country'];
    let map = new Map().setIn(['user', arr], 'Poland');
    console.log(map.getIn(['user', arr]));
    

    2. Serialize the array using toString

    An alternative solution is to serialize the arrays using toString:

    const { Map } = require('[email protected]')
    
    let map = new Map().setIn(["user",["alex","country"].toString()],"Poland")
    map.getIn(["user",["alex","country"].toString()])
    

    Example snippet:

    if (['alex', 'country'].toString() === ['alex', 'country'].toString()) {
      console.log('The serialized arrays are equal');
    }

    But remember that serializing different arrays can yield the same string:

    if (['alex', 'country'].toString() === ['alex,country'].toString()) {
      console.log('Serializing different arrays can yield the same string');
    }

    3. Flatten the array

    Another approach is to flatten the path array:

    const { Map } = require('immutable');
    
    let map = new Map().setIn(['user', ['alex', 'country']].flat(), 'Poland');
    console.log(map.getIn(['user', ['alex', 'country']].flat()));