Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Does immutablejs allow searching with a reference type

I have a piece of code that looks like this

const { Map } = require('immutable@4.0.0')

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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('immutable@4.0.0')

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()));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!