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.

2
  • 1
    2.2 million elements i doubt that . There is a cap limit on the array element. Still you can look at this solution stackoverflow.com/questions/2858121/… which has already answered on conversion Commented Jan 16, 2016 at 11:39
  • @Nirus. Really, goal is to scrape about 2.2m names from a website and they'll be in the above format Commented Jan 17, 2016 at 5:27

1 Answer 1

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>

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

1 Comment

But what how do you get it from the file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.