Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda