I can delete dataValues without any problems, but when I try to delete dataCategories, I get the error Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'slice'). The data I am trying to delete is of the same type for both dataValues and dataCategories;
datasetX: ['var1Value']
datasetY: ['meas1Value']
yAxisData: Array(1)
0: (5) [2, 10, 4, 20, 8]
xAxisData: Array(1)
0: (5) [5, 8, 10, 15, 20]
Here is my remove function ;
const removeThisGroup = (itemToRemove) => {
const chart = findChartByType(chartData, itemToRemove.chartName);
const groupId = itemToRemove.id;
const type = itemToRemove.type;
if (type === 'dataValues') {
const indexToRemove = chart.dataValues.findIndex((inputGroup) => inputGroup.key === groupId);
if (indexToRemove !== -1) {
chart.data[0].datasetY.splice(indexToRemove, 1);
chart.data[0].yAxisData.splice(indexToRemove, 1);
}
chart.dataValues = chart.dataValues.filter((inputGroup) => inputGroup.key !== groupId);
} else if (type === 'dataCategories') {
const indexToRemove = chart.dataCategories.findIndex((inputGroup) => inputGroup.key === groupId);
if (indexToRemove !== -1) {
chart.data[0].datasetX.splice(indexToRemove, 1);
chart.data[0].xAxisData.splice(indexToRemove, 1);
}
chart.dataCategories = chart.dataCategories.filter((inputGroup) => inputGroup.key !== groupId);
}
const chartIndex = chartData.findIndex((c) => c.chartName === chart.chartName);
const updatedChartData = [...chartData];
updatedChartData[chartIndex] = chart;
setChartData(updatedChartData);
};
I checked the indexToRemove, chart.data[0].datasetX and chart.data[0].xAxisData values and they are correct.