0

I need to push data into a global variable when a button is pressed. However, even though i created a local variable to copy the data i want to push, it continues to overwrite the global array (all previous value turn into the most recently pushed one).

global variables:

  • tjoints = [] - list of joints dicts
  • joints = {} - dict of numbers - defined by the user on the web browser

This is my code:

tableAdd(){

  if (this.time == undefined) {
    this.flashMessage.show('Duração do tempo faltando', {cssClass: 'alert-danger'});
  } else if(this.time == 0) {
    this.flashMessage.show('Duração precisa ser maior que Zero', {cssClass: 'alert-danger'});
  } else {
    var temp = this.joints;  //----> copy of the global variable
    var i = this.tjoints.push(temp); // pushing temp into the global array
    this.tjoints[i-1][9] = this.time;
    console.log('this.tjoints: ', this.tjoints);
  }
}

html:

<a class="btn btn-info" style="display: inline-block (click)="tableAdd()">Add</a>
  • Your data structure seems very complex array of arrays? – Vivz Jul 26 '17 at 15:29
  • Where do you copy the global variable to local scope? – sjahan Jul 26 '17 at 15:29
  • @Vivz It's actually an array of dicts, a json file – luizapozzobon Jul 26 '17 at 15:30
  • I copy the global variable to local scope inside the 'else' var temp = this.joints; – luizapozzobon Jul 26 '17 at 15:30
  • 1
    Oh excuse me (`this.` does not feel very global to me, but let's consider this as global). You do NOT copy anything, you assign the same object to another variable/name. That's why your global object is modified. It's still the global object, with a local reference to it. – sjahan Jul 26 '17 at 15:32
  • In var i you are pushing an array and here again this.tjoints[i-1][9] = this.time; where i is what?? – Vivz Jul 26 '17 at 15:33
  • @sjahan okay, and how can i fix it? – luizapozzobon Jul 26 '17 at 15:34
  • @Vivz 'i' is 1, 2, 3..., it increases according to how many pushes the user made in the session. These numbers are the array positions – luizapozzobon Jul 26 '17 at 15:35
  • @lulpenguin By asking nicely to Google of course ;) Here are pages that you could find interesting: https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript or https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object – sjahan Jul 26 '17 at 15:36
  • 'it continues to overwrite the global array', which means tjoints. I don't think this should be happening i.e. overriding the complete array by just pushing one value to it. It may be getting modified somewhere else otherwise. This code snippet doesn't have anything that can cause this issue of overriding n array elements by the one pushed at last. Push operation just appends element to array. So if there are 5 elements already it will push the 6th element to it. What does 9 represent in this.tjoints[i-1][9] ? – SKSpall Jul 26 '17 at 16:10
  • @SKSpall The 9 represents the key '9' of the joints array. – luizapozzobon Jul 26 '17 at 17:10

0 Answers0