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:
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);
}