0

I working in d3.js and I created this object with the values I need. Then when I go to draw the d3 line, the values have mysteriously changed to the 0 value when I originally initiated the value.

It is so odd, because I can do console.log(d) and depending on if the object is collapsed or expanded in the console it will show different values for d.my_count.

See this screenshot of the spooky behavior: enter image description here

Here is how I'm incrementing(changing) those values:

dataset.then(function(data) {
    //loop through all the data
    data.map(function (d){
        //round the score
        var rating = Math.round(d.average_rating)
        //bump up max rating if needed
        if(rating > maxRating){maxRating = rating};

        if(years.includes(d.year)){
            tmpIndex = indexByYear(d.year)
            ratingCountByYear[tmpIndex][rating]['my_count'] += 1;
        }
    })
})

See the line that says: ratingCountByYear[tmpIndex][rating]['my_count'] += 1;? That is the line that apparently makes the change to my ratingCountByYear object. But it will only show up in the console as the changed value WHEN EXPANDED.

This is the console in Firefox. Chrome is exhibiting a similar behavior, except the expanded and collapsed view show the values I want, while trying to access the values always gives me the instantiated value (I know because I've tried to change the instantiation, and only the instantiated value comes across when I try and access it)

What in the world is going on? I assume some sort of issue with how the objects are kept in memory under the hood?!?!?

b-ryce
  • 5,752
  • 7
  • 49
  • 79
  • 1
    with Objects in the console, the console shows the currently evaluated value of whatever you log - so you could console.log an array when it's empty, then push some values to it, and the console will show the items when you expand the object - this can also happen when you have some asynchronous code that populates an object or something ... but when you console.log, the asynchronous code hasn't done its job yet, but in the console it looks like the asynchronous data is there ... depends on your code as to why you see what you see – Jaromanda X Sep 24 '20 at 02:35

1 Answers1

1

I figured it out. I was trying to draw the lines before the data was finished being processed, and it wasn't updating. I put all my drawing into a function that I called once I was done data processing and that did the trick.

And for the console, the originally printed line (not expanded) was showing the initialized values, and expanded was the updated values.

b-ryce
  • 5,752
  • 7
  • 49
  • 79