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

297
Vistas
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 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