I am developing an online tool to display information in an organized dashboard interface. I am pulling the data from a CSV spreadsheet and separating them into another multiD array depending on one field in the overall CSV data array.
This is what I have tried:
// arr is the array that holds the entire CSV data
// inds is where I need the entries separated by industry (arr[i][0])
var indx = 0; // using to add new array to inds array
for(var i = 1; i < arr.length-1; i++){
if(arr[i][0] === arr[i-1][0]) {
if(! inds[indx] ) {
inds[indx] = []
} else {
inds[indx].push(arr[i]);
}
} else {
indx++;
}
}
Basically what this does:
If the industry field is the same as the entry before, throw it into the arr array with the index of indx, otherwise, add one to the indx for the next industry.
The problem with this solution:
It skips the 1st entry of the industry, because the row before was a different industry, so the arr[i-1][0] is checking the last industry and skips the 1st row of the next industry.
How can I reformat this script:
I was thinking maybe nested for/while, but not sure. The way I am thinking of the logic is:
For each row of the csv, while industry column is the same, toss into respective inds[index]. Or, extract rows from arr by the same value of arr[i][0].
I am sure there is an easier solution, and that is why I am here, so I hope I got my thoughts across clearly and I appreciate any and all feedback.
If you need more clarification, just let me know in the comments.