Results 1 to 2 of 2

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

  1. #1
    Join Date
    Jan 2013

    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

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

    to parse a CSV:

    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;}
      ob.push(float); stack.push(ob);
    return stack;
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

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