How To Read A Text File Into An Array Using JavaScript

To read a text file into an array using JavaScript, We use NodeJs built-in methods to read files and combine them with JavaScript’s built-in methods to convert a text file into an array using JavaScript. Read the tutorial below to understand how we guide you.

Two ways to Read A Text File Into An Array Using JavaScript

After researching and researching, we found the perfect combination to guide you:

  • Use readFileSync() and split() methods
  • Use readFile() and split() methods

Use readFileSync() and split() methods

readFileSync() method

This method is used to read the file and return its contents.

Syntax

fs.readFileSync(path, options)

Parameters

  • path: Is the path of the file
  • options: An optional parameter for encoding and flag

The split() method is used a lot by us to convert from a string to an array.

File contents.txt

The learnshareIT site is very useful

Example

const fs = require("fs");

function readFileContents(path) {
    // Read the contents of the file
    const fileData = fs.readFileSync(path, "utf-8");

    // Convert file contents to an array
    const result = fileData.split(/\s+/);
    console.log(result);
}

readFileContents("./contents.txt");

Output

['The', 'learnshareIT', 'site', 'is', 'very', 'useful']

To read a text file into an array using JavaScript, We use the readFileSync() method from the fs module to read the file and then the split() method to convert the read content to an array with a regular expression as the parameter.

We will explain regular expressions to you to understand why I use such characters.

  • ‘\s’: any character is a space.
  • ‘+’: repeat the previous character 1 or more times.

Use readFile() and split() methods

readFile() method

Used to asynchronously read the contents of a file/

Syntax

fsPromises.readFile(path, options)

Parameter

  • path: Is the path of the file
  • options: An optional parameter for encoding and flag

File contents.txt

The learnshareIT site is very useful

Example

const fsPromises = require("fs").promises;

async function readFileContents(path) {
    // Read the contents of the file
    const fileData = await fsPromises
        .readFile(path, "utf-8")
        .then((data) => {
            // Convert file contents to an array
            var result = data.split(/\s+/);
            console.log(result);
        })
        .catch((error) => {
            console.log(error);
        });
}

readFileContents("./contents.txt");

Output

['The', 'learnshareIT', 'site', 'is', 'very', 'useful']

This tutorial shows you another way of reading a file asynchronously using the readFile() method from the fs promises module. We combine it with the built-in split() method to pass read content to an array. This readFile() method will return a promise.

Summary

This tutorial used and combined NodeJS and JavaScript built-in methods to read a text file into an array using JavaScript. We have a lot of tutorials like this; it’s effortless for you to understand the entire content. Follow our page to see more articles like this.

Leave a Reply

Your email address will not be published. Required fields are marked *