Skip to content Skip to sidebar Skip to footer

Can Readfilesync From Node.js Fs Library Load An Array From A Text File?

My text file contains one small array (for simplicity here) of two objects as shown in array1. I want to load this text file and manipulate the array in further downstream steps. W

Solution 1:

If smallArray.txt contains valid JSON, all you need to do is parse the contents:

array1 = JSON.parse(fs.readFileSync('smallArray.txt', 'utf8'));

Solution 2:

If the data in the file is valid JSON and you rename smallArray.txt to smallArray.json, you can just use require() and it will automatically parse it like this:

var array1 = require('smallArray.json');

Post a Comment for "Can Readfilesync From Node.js Fs Library Load An Array From A Text File?"