[Chartjs]-Uncaught TypeError: [Array].filter is not a function

0👍

Your code seems to work properly as you can see here

You might want to refactor it to be sure to have access to the data in search results when you need it:

var queryUrl = "http://127.0.0.1:5000/api/1/GenderOverTime";
var searchResults = [];

d3.json(queryUrl).then(function (chart_data) {
  searchResults = chart_data;
  init(searchResults);
});


function init(results) {
  console.log("searchResults: ", results);
  let selector = d3.select("#selDataset");
  let options = [];
  filtered_data_for_chart = results.filter((result) => {
    if (!options.includes(result.Sport)) {
      options.push(result.Sport);
      selector
        .append("option")
        .text(result.Sport)
        .property("value", result.Sport);
    }

    return result.Sport === "Gymnastics";
  });
}

1👍

You can use Array.from() to create a new, shallow-copied Array instance from an array-like or iterable object. So this should solve your issue:

const searchResultsList = Array.from(searchResults)
filtered_data_for_chart = searchResultsList.filter(result=>{...})

Leave a comment