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

300
Visualizações
How to assign a value to a mutable reference of an object property?

Consider the following :

let data = {
  "world": "bc",
  "d": "ef",
  "g": "hi",
};
let panel = {
  "hello": {
    "world": {},
  }
};
let dataTree = panel.hello;
let exampleBool = true;

if (exampleBool) {
  dataTree = panel.hello.world;
}
Object.assign(dataTree, data);
dataTree.world = "xz";

console.log(dataTree.world);
console.log(data.world);
console.log(data === dataTree);

Output :

xz
bc
false

Desired output :

xz
xz
true

I would like to set a value to the object property referenced by dataTree, which can change depending on exampleBool. I would like to link the object referenced by data be the same as dataTree, in other word makes dataTree === data to be true.

The problem with Object.assign is that it makes a copy of each property in data and in my case, data keys can be a thousands length so I don't want to clone each ones.

Any way to do this in JS ?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I would like to set a value to the object property referenced by dataTree

The variable dataTree is not a reference to an object property. There is no such thing in JS. dataTree does reference the { "world": {} } object (or, after the conditional assignment, the {} object), and that's it. You cannot mutate the property where you get the object from through the variable.

Instead, store the base object and the property name:

let data = {
  "world": "bc",
  "d": "ef",
  "g": "hi",
};
let panel = {
  "hello": {
    "world": {},
  }
};
let dataTreeBase = panel,
    dataTreeProp = "hello";
let exampleBool = true;

if (exampleBool) {
  dataTreeBase = panel.hello;
  dataTreeProp = "world";
}
dataTreeBase[dataTreeProp] = data;
dataTreeBase[dataTreeProp].world = "xz";

console.log(dataTreeBase[dataTreeProp].world);
console.log(data.world);
console.log(data === dataTreeBase[dataTreeProp]);

Of course there are various ways to abstract this in functions or objects with getters/setters or methods, but you cannot achieve this with just a plain variable.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can assign dataTree to data:

let data = {
  "world": "bc",
  "d": "ef",
  "g": "hi",
};
let panel = {
  "hello": {
    "world": {},
  }
};
let dataTree = panel.hello;
let exampleBool = true;

if (exampleBool) {
  dataTree = panel.hello.world;
}
data = dataTree;
dataTree.world = "xz";

console.log(dataTree.world);
console.log(data.world);
console.log(data === dataTree);

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