node.js: read a text file into an array. (Each line an item in the array.)
If you can fit the final data into an array then wouldn't you also be able to fit it in a string and split it, as has been suggested?
In any case if you would like to process the file one line at a time you can also try something like this:
var fs = require('fs');
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
function func(data) {
console.log('Line: ' + data);
}
var input = fs.createReadStream('lines.txt');
readLines(input, func);
EDIT: (in response to comment by phopkins) I think (at least in newer versions) substring does not copy data but creates a special SlicedString object (from a quick glance at the v8 source code). In any case here is a modification that avoids the mentioned substring (tested on a file several megabytes worth of "All work and no play makes Jack a dull boy"):
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
How to read a file line by line in Javascript and store it in an array
Maybe this will help you.
Async Version:
const fs = require('fs')
const emails = [];
const names = [];
fs.readFile('file.txt', (err, file) => {
if (err) throw err;
file.toString().split('\n').forEach(line => {
const splitedLine = line.split(':');
emails.push(splitedLine[0]);
names.push(splitedLine[1]);
});
});
Sync Version:
const fs = require('fs')
const emails = [];
const names = [];
fs.readFileSync('file.txt').toString().split('\n').forEach(line => {
const splitedLine = line.split(':');
emails.push(splitedLine[0]);
names.push(splitedLine[1]);
})
console.log(emails)
console.log(names)
How to turn data from a txt file into an array of objects in Node
You can try readline
internal module, by the way (see this example), if your file is big and you do need line by line processing:
const fs = require('fs');
const readline = require('readline');
async function processLineByLine() {
const fileStream = fs.createReadStream('test.txt');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
let headers = null;
const data = [];
for await (const line of rl) {
const row = line.split(/\s+/);
if (headers === null) { // So this is the first line.
headers = row;
} else {
const entry = {};
for (let i = 0; i < row.length; i++) {
const header = headers[i];
const cell = row[i];
entry[header] = header === 'Date' ? cell : Number(cell);
// Or more generally:
// const mayBeNumber = Number(cell);
// entry[header] = Number.isNaN(mayBeNumber) ? cell : mayBeNumber;
}
data.push(entry);
}
}
console.log(data);
}
processLineByLine();
Reading local text file into a JavaScript array
Using Node.js
sync mode:
var fs = require("fs");
var text = fs.readFileSync("./mytext.txt");
var textByLine = text.split("\n")
async mode:
var fs = require("fs");
fs.readFile("./mytext.txt", function(text){
var textByLine = text.split("\n")
});
UPDATE
As of at least Node 6, readFileSync
returns a Buffer
, so it must first be converted to a string in order for split
to work:
var text = fs.readFileSync("./mytext.txt").toString('utf-8');
Or
var text = fs.readFileSync("./mytext.txt", "utf-8");
How do I read an array in a file to an array variable in node.js?
You have to decide what storage format you want to use in your text file. A Javascript object is an internal format. A typical format to use for Javascript objects is JSON. Then, you need to convert TO that format when saving and parse FROM that format when reading.
So, in Javascript, JSON.stringify()
converts a Javascript object/array into a JSON string. And, JSON.parse()
converts a JSON string into a Javascript object/array.
You can write your array in JSON using this:
fs.writeFile("./array.txt", JSON.stringify(array), function(err) {
if(err) {return console.log(err);}
});
And, you can read in JSON like this:
try {
let array = JSON.parse(fs.readFileSync("./array.txt"));
// use your array here
} catch(e) {
console.log("some problem parsing the JSON");
}
Read a file one line at a time in node.js?
Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here's the easiest way to read lines from a file, without any external modules:
const fs = require('fs');
const readline = require('readline');
async function processLineByLine() {
const fileStream = fs.createReadStream('input.txt');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
console.log(`Line from file: ${line}`);
}
}
processLineByLine();
Or alternatively:
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
lineReader.on('line', function (line) {
console.log('Line from file:', line);
});
The last line is read correctly (as of Node v0.12 or later), even if there is no final \n
.
UPDATE: this example has been added to Node's API official documentation.
Related Topics
Check If Object Is a Jquery Object
Can't Throw Error from Within an Async Promise Executor Function
Jquery - Get Text for Element Without Children Text
Scaling Socket.Io to Multiple Node.Js Processes Using Cluster
Uncaught Typeerror: Cannot Read Property 'Value' of Null
Node.Js Tail-Call Optimization: Possible or Not
How to Get Class Object's Name as a String in JavaScript
Canvas Image Crossplatform Insecure Error
(Deep) Copying an Array Using Jquery
How to Convert String to Number According to Locale (Opposite of .Tolocalestring)
How to Smoothly Scroll to an Element in Pure JavaScript
Componentdidmount Equivalent on a React Function/Hooks Component
How to Remove Spaces from a String Using JavaScript
Window.Onunload Is Not Working Properly in Chrome Browser. Can Any One Help Me