[Chartjs]-Can't figure out why the values in my objects are changing

7đź‘Ť

âś…

Part of your problem stems from pushing the same object into the 2 different arrays here:

    for(i = 0; i < languages.length; i++) {
        currentLanguage = new Language(languages[i].language)

        learningLanguages.push(currentLanguage)
        nativeLanguages.push(currentLanguage)
   }

This does not copy currentLanguage into each, it pushes references of the same object into each.

Then whatever you do to that object reference in one will be reflected in the other

Try making 2 separate objects

    for(i = 0; i < languages.length; i++) { 
        learningLanguages.push(new Language(languages[i].language))
        nativeLanguages.push(new Language(languages[i].language))
   }

the use of global variables will also get you into trouble…don’t do it!

1đź‘Ť

learningLanguages = []
nativeLanguages = []

These two variables look like they are not defined in an above scope – thus the second XHR call does not know about these variables.

Second piece of the answer is reference to the same object instance, with the same “count” attribute touched twice.

I’d suggest two options are here:

  • use new Language(...) for each of these two arrays separately
  • or have a separate counter for each type of native/learning counts.

Leave a comment