简体   繁体   English

CSV node.js,从文件到数组

[英]CSV node.js, from file to array

I've been trying for ages now to no avail (some just returning 'null'). 我已经尝试了很多年了,但一直没有用(有些只是返回“ null”)。 I'm trying to get this CSV (which will eventually contain over 2.2 million elements): 我正在尝试获取此CSV(最终将包含超过220万个元素):

Rough,Lukk,black,Semnas,mickayrex 粗糙,Lukk,黑色,Semnas,mickayrex

to be read into a javascript array. 被读入javascript数组。

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. 并且目标是能够将文本文件中的所有名称放入上述数组格式中,然后由程序的其余部分使用。

Here is a function from the book "JavaScript functional programming" for parse CSV to an array (I have modified it to avoid use underscore): 这是《 JavaScript函数编程》一书中的函数,用于将CSV解析为数组(我已对其进行了修改,以避免使用下划线):

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM