0

I've been trying for ages now to no avail (some just returning 'null'). I'm trying to get this CSV (which will eventually contain over 2.2 million elements):

Rough,Lukk,black,Semnas,mickayrex

to be read into a javascript array.

Currently, I just have a fixed array

var usernames = ['mickayrex', 'black'];

and the goal is to be able to put all names from the text file into an array format as above to then use by rest of the program.

Miniman
  • 221
  • 1
  • 4
  • 16
  • 1
    2.2 million elements i doubt that . There is a cap limit on the array element. Still you can look at this solution http://stackoverflow.com/questions/2858121/convert-comma-separated-string-to-array which has already answered on conversion – NiRUS Jan 16 '16 at 11:39
  • @Nirus. Really, goal is to scrape about 2.2m names from a website and they'll be in the above format – Miniman Jan 17 '16 at 05:27

1 Answers1

1

Here is a function from the book "JavaScript functional programming" for parse CSV to an array (I have modified it to avoid use underscore):

function parseCSV(str) {
  var rows = str.split('\n');

  return rows.reduce(function(table, row) {
    var cols = row.split(',');

    table.push(cols.map(function(c) {
      return c.trim();
    }));

    return table;
  }, []);
}

var frameworks = 'framework, language, age\n ' +
                 'rails, ruby, 10\n' +
                 'node.js, javascript, 5\n' +
                 'phoenix, elixir, 1';

var array = parseCSV(frameworks);
console.log(array);

var consoleEl = document.querySelector('#console');
consoleEl.innerHTML = 'Array content: '+array;
<div id="console"></div>
gabrielperales
  • 6,935
  • 1
  • 20
  • 19