Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!