0

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.

1
  • There are no "multiD array"s in these languages; you can only nest arrays (strictly speaking, an element of an array can be a reference to another Array instance), which is a different thing. It is unclear to me from your description what the code is intended to do. Please provide an example of the expected input and output data so that solutions to the problem can be suggested instead of fixes to your code. Commented Feb 4, 2013 at 18:48

1 Answer 1

3

Does the following logic work?

var indx = -1;
 // 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]) {
                indx++;
                inds[indx] = []
            }
            inds[indx].push(arr[i]);
        }

Updated.

Sign up to request clarification or add additional context in comments.

3 Comments

(and start with indx = -1 instead of 0;)
that did work a bit better. but it is still skipping the very 1st instance of the industry in the field arr[i][0]
oooo.. that console.log was sexy! perfect! thank you so much! been banging my head on my desk trying to get this to work correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.