www.webdeveloper.com
Results 1 to 2 of 2

Thread: HELP!!! How to convert CSV file into array[x][y] formation

  1. #1
    Join Date
    Feb 2012
    Posts
    1

    HELP!!! How to convert CSV file into array[x][y] formation

    PLEASE HELP!!!

    Below, I have following csv data from excel:

    LV, JOB, NAME, HP, SP
    11, Fighter, Herran, 200, 1000
    22, Warrior, Jimmy, 400, 200
    12, Priest, Kim, 500, 300
    33, Berserker, Lee, 1200, 100
    12, Wizard, Adam, 50, 2000

    I wanted to create a array like:

    array[0][0]=11;
    array[0][1]=Fighter;
    array[0][2]=Herran;
    array[0][3]=200;
    array[0][4]=1000;

    array[0][0]=22;
    array[0][1]=Warrior;
    array[0][2]=Jimmy;
    array[0][3]=400;
    array[0][4]=300;
    .
    .
    .

    Please kindly suggest and help.

    Depend on situation, there can be extra 100 rows...

    Regards,
    James Ma

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    Make an Array by splitting the csv text on new lines,
    then split each item on commas

    Code:
    function arrayFromCsv(csv){
        var A= csv.split('\n'), L= A.length, i= 0;
        while(i<L){
            A[i]= A[i].split(/, */);
            ++i;
        }
        return A;
    }
    var csvarray= arrayFromCsv(csvtext)
    
    /*   value: Array of Arrays
        [
            ['LV', 'JOB', 'NAME', 'HP', 'SP'],
            [11, 'Fighter', 'Herran', 200, 1000],
            [22, 'Warrior', 'Jimmy', 400, 200],
            [12, 'Priest', 'Kim', 500, 300],
            [33, 'Berserker', 'Lee', 1200, 100],
            [12, 'Wizard', 'Adam', 50, 2000]
        ]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles