-3

I want to be able to read a file for example names.txt and for example in the text file on each line is NAME:LAST and it on each line. I want to split each so I can use NAME = {0} LAST = {1}

Example names.txt

NAME1:LAST1
NAME2:LAST2
NAME3:LAST3
NAME4:LAST4
SlickNick
  • 1
  • 1

1 Answers1

0

Well this should help you...

const {readFile} = require('fs');

readFile('names.txt', 'utf8', (err, str) => {
    if (err) {
        throw err;
    }
    str.split('\n').forEach(item => {
        const fullName = item.split(':');
        let name = fullName[0];
        let last = fullName[1];
        console.log(name, last);
    });
});
Bharath
  • 390
  • 1
  • 6