www.webdeveloper.com
Results 1 to 2 of 2

Thread: Convert .csv or .txt to Javascript .js array

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    1

    Convert .csv or .txt to Javascript .js array

    Hello all,
    I am not a coder but I have to make some revisions to a .js array that is used for a special search on my website. I have 3 columns of data in 25,000 rows yielding approximately 45000 unique combinations.
    The syntax of the .js array is;
    level1=new Array;level0=new Array("x1","x2","x3",.......);level1["x1"]=new Array("x1y1","x1y2","x1y3",......);level2["x1y1"]=new Array("z1","z2","z3",.....).....

    I have tried to manipulate the data in excel and notepad++ but cant find a simple solution. Is there a conversion tool available to get this done or do I need a custom conversion tool?

    Thanks in advance for any help sent my way
    Cheers!

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    if you have an array of arrays:
    Code:
    Array.prototype.toCSV=function(){ return this.join("\n"); };

    to parse a CSV:

    Code:
    function parseCSV(str){
    var  stack=[], ob=[], float="", inQuotes=false;
    for( var i=0; i<str.length;i++){
      var it=str[i];
        if(it==='"'){  inQuotes=!inQuotes; if(str[i-1]==='"'){float+='"';} continue; }
        if(it===","){ if(!inQuotes){ob.push(float); float="";}else{float+=",";} continue;}
        if(it==="\n"){ ob.push(float); stack.push(ob); float=""; ob=[]; continue;}
        float+=it;
      }
      ob.push(float); stack.push(ob);
    return stack;
    }

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