Meghana V
Meghana V

Reputation: 11

How to convert a local text file to an array and display its elements using javascript

abc.txt

John 22-08-1998 Bangalore 1111111111

const k;
this.k = sessionStorage.getItem('loggedUser');
if (k.UpperCase() === 'MEGHANA') {
  const fs = require('fs')
  const text = fs.readFileSync('./MEGHANA.txt').toString('utf-8')
  const textByLine = text.split('\n')
  document.write(textByLine[0])
}

Upvotes: 0

Views: 58

Answers (1)

soheethewebdeveloper
soheethewebdeveloper

Reputation: 81

I hope this code is what you mean!

const k;
this.k = sessionStorage.getItem('loggedUser');
if (k.UpperCase() === 'MEGHANA') {
  const fs = require('fs');
  const text = fs.readFileSync('./MEGHANA.txt').toString('utf-8');
  const textByLine = text.split('\n');
  const textArray = textByLine[0].split(' ');
  textArray.forEach(t => document.write(t));
}

Upvotes: 1

Related Questions