1

I am trying to split a csv file into a groups based on a vlue within the first column. Then store the rows in an array for displaying on a web page. I'm new to javascript and can't figure it out. I have a csv file with the following structure:

0,-1.0,0.0,0.0,0.0
1,0.0,0.0,0.0,0.0
0,-1.0,0.0,0.0,0.0
1,-1.0,0.0,0.0,0.0
2,0.0,0.0,0.0,0.0
0,-1.0,0.0,0.0,0.0
1,-1.0,0.0,0.0,0.0
2,-1.0,0.0,0.0,0.0
3,0.0,0.0,0.0,0.0
0,-1.0,0.0,0.0,0.0
1,-1.0,0.0,0.0,0.0
2,-1.0,0.0,0.0,0.0
3,-1.0,0.0,0.0,0.0
4,0.0,0.0,0.0,0.0

I am trying to split the data rows into groups based on the values of the first column in the file. I am trying to group from 0 up to the next zero then have each block of split data stored in an array for displaying on a web page.

I am able to access the first row from a for loop and set a condition to check if the value is 0, but i'm am having trouble adding the next rows up until the next 0 is dectected.

Please see below for what i have so far.

// AJAX in the data file
    $.ajax({
        type: "GET",
        url: "static/Dataframe.csv",
        dataType: "text",
        cache: false,
        success: function(data) {
            var arr1 = data.split(/\r\n|\n|\r/);
            var array2 = [];
            for (let i = 0; i < arr1.length; i++) {

                    arr1[i].split(',');
                    array2.push(arr1[i]);
                    if(arr1[i][0]== 0){
                        array2.pop()
                    }
            }
            // for debugging
            // document.getElementById("dialog").innerHTML = arr1;
            console.log(array2);
        }
    });

This is the result i am getting so far.

[0: "1,0.0,0.0,0.0,0.0"
1: "1,-1.0,0.0,0.0,0.0"
2: "2,0.0,0.0,0.0,0.0"
3: "1,-1.0,0.0,0.0,0.0"
4: "2,-1.0,0.0,0.0,0.0"
5: "3,0.0,0.0,0.0,0.0"
6: "1,-1.0,0.0,0.0,0.0"
7: "2,-1.0,0.0,0.0,0.0"
8: "3,-1.0,0.0,0.0,0.0"
9: "4,0.0,0.0,0.0,0.0"]

What i need it to be is:

[
    [0, -1.0, 0.0, 0.0, 0.0, 1, 0.0, 0.0, 0.0, 0.0],
    [0, -1.0, 0.0, 0.0, 0.0, 1, -1.0, 0.0, 0.0, 0.0, 2, 0.0, 0.0, 0.0, 0.0],
    [0, -1.0, 0.0, 0.0, 0.0, 1, -1.0, 0.0, 0.0, 0.0, 2, -1.0, 0.0, 0.0, 0.0, 3, 0.0, 0.0, 0.0, 0.0],
    [0, -1.0, 0.0, 0.0, 0.0, 1, -1.0, 0.0, 0.0, 0.0, 2, -1.0, 0.0, 0.0, 0.0, 3, -1.0, 0.0, 0.0, 0.0, 4, 0.0, 0.0, 0.0, 0.0]
]

I'm new to javaScript and I'm having trouble figuring this out. Any advice would be greatly appreciated.

2
  • 1
    does something work? please add a wanted result as well. Commented Jan 21, 2019 at 16:16
  • Sorry I ment to put that in. I'll do that now. Commented Jan 21, 2019 at 16:17

3 Answers 3

1

You could check the first value of the inner arrays and add a new array to the result set.

var data = '0,-1.0,0.0,0.0,0.0\n1,0.0,0.0,0.0,0.0\n0,-1.0,0.0,0.0,0.0\n1,-1.0,0.0,0.0,0.0\n2,0.0,0.0,0.0,0.0\n0,-1.0,0.0,0.0,0.0\n1,-1.0,0.0,0.0,0.0\n2,-1.0,0.0,0.0,0.0\n3,0.0,0.0,0.0,0.0\n0,-1.0,0.0,0.0,0.0\n1,-1.0,0.0,0.0,0.0\n2,-1.0,0.0,0.0,0.0\n3, -1.0, 0.0, 0.0, 0.0\n4, 0.0, 0.0, 0.0, 0.0',
    result = data.split(/\r\n|\n|\r/).reduce((r, s) => {
        var array = s.split(',').map(Number);
        if (!array[0]) {
            r.push([]);
        }
        r[r.length - 1].push(...array);
        return r;
    }, []);

console.log(result.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

Thank you Nina. This is exactly what I'm looking for :)
0

use this

// AJAX in the data file
    $.ajax({
        type: "GET",
        url: "static/Dataframe.csv",
        dataType: "text",
        cache: false,
        success: function(data) {
            var arr1 = data.split(/\r\n|\n|\r/);
            var array2 = [];
            for (let i = 0; i < arr1.length; i++) {
                    tmp =  arr1[i].split(',');
                    array2.push(tmp);
                    if(arr1[i][0]== 0){
                        array2.pop()
                    }
            }
            // for debugging
            // document.getElementById("dialog").innerHTML = arr1;
            console.log(array2);
        }
    });

Comments

0
// AJAX in the data file
// Just removed the for loop with the length and see the modified code.
$.ajax({
        type: "GET",
        url: "static/Dataframe.csv",
        dataType: "text",
        cache: false,
        success: function(data) {
            var arr1 = data.split(/\r\n|\n|\r/);
            var array2 = [];
            for (var items of arr1) {
                    tmp =  items.split(',');
                    array2.push(tmp);
                    if(items[0]== 0){
                        array2.pop()
                    }
            }
            // for debugging
            // document.getElementById("dialog").innerHTML = arr1;
            console.log(array2);
        }
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.