Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda