Trying to apply filtering on a JSON data array structured like this:
var data = [
{
"key": "FirstGroup",
"color": "#1f77b4",
"values": [
{
"label": "PWY-6089",
"value": 0.0
},
{
"label": "TOLSULFDEG-PWY",
"value": 0.0
},
]
},
{
"key": "SecondGroup",
"color": "#78bf00",
"values": [
{
"label": "PWY-4101",
"value": 0.3
},
{
"label": "PWY0-1356",
"value": 0.5
}
]
},
...
]
The goal is to have a subset of stackedChartData in filteredData where the filterString is found as a substring within "label". This is the proposed method:
function startsWith(str, word) {
return str.lastIndexOf(word, 0) === 0;
}
function filterData() {
input = document.getElementById('filterInput');
filterString = input.value;
var filteredData = stackedChartData.filter(function (entry) {
entry.values.forEach(element => {
return startsWith(element.label,filterString);
});
});
console.log(filteredData,"filteredData");
}
Due to issues with String.startsWith(), the startsWith() function was used instead in the script. Despite correct definitions for filterString and element.label, the filteredData remains empty. Any guidance on rectifying this confusion would be greatly appreciated. Best regards.