Apologies for the simplicity of my question and my limited knowledge in javascript. I am completely new to this language and need some guidance. I have an array of data and I want to extract a subset based on selected columns. Here is a snippet of the initial data:
0: {ID: 3607, Name: 'Alamo', Funds: 52933955, Revenues: 9160109, BAT: 5, …}
1: {ID: 3539, Name: 'Alvin', Funds: 6128147, Revenues: 964083, BAT: 0, …}
2: {ID: 3540, Name: 'Amarillo', Funds: 12450969, Revenues: 1716038, BAT: 0, …}
I aim to create a new array from columns 0, 1, 2, and 4 (ID, Name, Funds, and BAT). In the provided code block, toolData
represents the original dataset ('toolData.json') while tableData
indicates the new array being formed. The selections
variable holds the numbers of the desired columns.
var getData = () => axios.get('toolData.json')
.then(res => res.data)
.then(data => {
var toolData = data;
console.log(toolData);
var tableData = [];
var selections = [0,1,2,4];
for (i=0; i < toolData.length; i++)
{
tableData[i] = toolData[i];
for (j=0; j<selections.length; j++)
{
k = selections[j],
tableData[i][j] = toolData[i][k]
}
}
console.log(tableData);
This specific code excerpt seems to be causing issues as it appears to result in an infinite loop. By removing tableData[i] = toolData[i];
, the loop issue vanishes but the code still doesn't function correctly. While console.log(toolData);
outputs the complete dataset as expected, console.log(tableData);
displays an error message:
javascript.js:42 Uncaught (in promise) TypeError: Cannot set properties of undefined (setting '0')
at javascript.js:42
My ultimate goal is to allow users to select the columns they wish to include in the new array. However, before implementing that feature, I must address this current challenge.