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

298
Visualizações
I pass an array by reference to another class, but it doesn't get modified there

I have a problem with a web app I'm creating. In short, I want to pass an array by reference, so that change in a module will affect the array in the main class. The way I do it is:

  1. I send the array as a function parameter and assign it in the module
  2. I trigger the change in the main class, and the array is updated in the module
  3. When I update the array in the module, it is not updated in the main class.

I don't understand it, array should be passed by reference, and apparently it is as the change works one way, but not the other. I log the values on both sides and see that it works. It's driving me nuts...

Code boils down to this, these are the only places where arrays are affected.

Main class:

import { build } from "Module.js";

let usedRelations = [];

window.onload = function () {
build(usedRelations);
}

function addNewRelation(relationId) {
let rel = {}; // instantiating relation object here
usedRelation.push(rel); // it affects the array in the module class
}

Module:

let relationsArray;

export function build(usedRelations) {
relationsArray = usedRelations; 
}

function deleteUnsavedRelation(usedRelation) {

//relationsArray is filtered properly, but usedRelations from main class remain unchanged
relationsArray = relationsArray.filter(x => x.relationId != usedRelation.relationId); 
}
about 4 years ago · Juan Pablo Isaza
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