0

I am new to javascript from C# and have a feeling that I am not quite understanding some of the fundamentals of how variable assignment and manipulation works. I have a function that runs to filter some results based on year and class group. This works as intended the first time the function is run.

thisSchoolArray is populated externally and retains it's values constantly. It is filteredArray that does not update as intended. I would imagine that each time the function runs, it puts all the results of the filter into filteredArray. Below is the code:

function UpdateResults() {
  var filteredArray = thisSchoolArray.filter(function (item) {
    return item.TestResults.some(e => e.TestYear === yearToDisplay);
  });
  console.log(filteredArray); //Works first time, empty next time the function runs
  var filteredClass = filteredArray.filter(x => x.ClassGroup === classToDisplay);
  filteredResults = filteredClass;
  console.log(filteredResults); //Works first time, empty next time the function runs
  table.setData(filteredResults);
  UpdateRoundScores();
}

Here is whats logged to the console: enter image description here

10
  • If filteredArray becomes empty on the second call, it means that thisSchoolArray or yearToDisplay has changed. It has either become an empty array itself or no longer has any elements matching yearToDisplay Commented Oct 11, 2019 at 0:52
  • 1
    I suspect the problem is this: stackoverflow.com/questions/4057440/…. Change your log statements to console.log(filteredArray.slice()) or console.log(JSON.stringify(filteredArray)), so you get a snapshot that doesn't change. Commented Oct 11, 2019 at 0:52
  • Either that, or yearToDisplay or classToDisplay are changing. Can you create a minimal reproducible example that demonstrates the problem? Commented Oct 11, 2019 at 0:53
  • Oh yeah, don't use console.log for debugging. Use your debugger instead ~ developers.google.com/web/tools/chrome-devtools/javascript Commented Oct 11, 2019 at 0:54
  • @Phil Isn't DevTools the same thing as the console? Commented Oct 11, 2019 at 0:55

1 Answer 1

1

Turns out that the yearToDisplay which I was changing via a dropdown was the culprit. The dropdown value would come in as a string when I was looking for an int to pass into the yearToDisplay variable. Another one of those simple but overlooked issues.The fix was

yearToDisplay = parseInt(dropdown.value)
Sign up to request clarification or add additional context in comments.

3 Comments

FYI, it's always recommend to use the 2nd arg for parseInt, ie parseInt(dropdown.value, 10)
Why would that be better @Phil? And since it's a year, should use parseInt(dropdown.value, 4) as there are only ever 4 numbers in a year?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.